在Stack Overflow post之后,我试图让div浮动,因为我向下滚动网页,但它不适合我。
以下是代码段
<script type="text/javascript">
window.onscroll = function (e) {
var vertical_position = 0;
if (pageYOffset)//usual
vertical_position = pageYOffset;
else if (document.documentElement.clientHeight)
vertical_position = document.documentElement.scrollTop;
else if (document.body)
vertical_position = document.body.scrollTop;
var your_div = document.getElementById('menuDiv');
your_div.top = (vertical_position + 200) + 'px';
}
</script>
答案 0 :(得分:3)
您根本不需要JavaScript,只需在CSS中使用position: fixed;
。
#menuDiv {
position: fixed;
top: 200px;
}