css: #anchor{ position:absolute;
top:40%; left:40%; width:100px; height:100px; background-color:red;}
js:$(function(){ var anchorTop = $("#anchor").css("top"); anchorTop = Number(anchorTop.substring(0, anchorTop.indexOf("p"))); //首先在监听器外部记录某id=anchor的标签的初始位置 window.onscroll = function () { var top = document.documentElement.scrollTop || document.body.scrollTop; $("#anchor").css({ transition:"top 1s", webkitTransition:"top 1s", oTransition:"top 1s", msTtransition:"top 1s", mozTransition:"top 1s", top: anchorTop + top + "px" }); };});
时间: 2024-10-28 01:08:32