div滚动到指定位置固定

首先将需要顶部固定的区域使用<div id=”nav_fuanfu”>包裹起来,然后在网页的特别底部放上以下的JS代码,用于控制这个区域顶部滚动固定。

 <script type="text/javascript" >
function menuFixed(id){
var obj = post.getElementById(id);
var _getHeight = obj.offsetTop;

window.onscroll = function(){
changePos(id,_getHeight);
}
}
function changePos(id,height){
var obj = post.getElementById(id);
var scrollTop = post.postElement.scrollTop || post.body.scrollTop;
if(scrollTop < height){
obj.style.position = 'relative';
obj.style.top = '0';
}else{
obj.style.position = 'fixed';
obj.style.top = '100px';
obj.style.width = '255px';
}
}
</script>
<script type="text/javascript">
window.onload = function(){
menuFixed('nav_fuanfu');
}
</script>

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容