小知识点,废话不多说,直接上代码
css:
<style> <BR>#anchor:{ <BR>position:absulate; <BR>top:40%; <BR>left:40%; <BR>width:100px; <BR>height:100px; <BR>background-color:red; <BR>} <BR></style>
var auchorTop = $("#anchor").css("top"); <br>auchorTop = Number(auchorTop.substring(0, anchorTop.indexOf("p"))); //首先在监听器外部记录某id=anchor的标签的初始位置 <br>window.onscroll = function () { <br>var top = document.documentElement.scrollTop || document.body.scrollTop; <br>$("#anchor").css("top", anchorTop + top + "px"); <br>};