DIV滚动到顶部固定在顶部 - 畅学网
博学强知,畅腾思维

DIV滚动到顶部固定在顶部

发布:Andre tian2014-11-26 13:11分类: 网页制作 标签: 网站建设

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    .wrapper{width:1000px;height:2000px;margin-left:auto;margin-right:auto;}
    .header{height:150px;}
    #nav{padding:10px;position:relative;top:0;background:black;width:1000px;}
    a{display:inline-block;margin:0 10px;*display:inline;zoom:1;color:white;}
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="header"></div>
        <div id="nav">
            <a href="#">畅学网</a>
            <a href="#">畅学网</a>
            <a href="#">畅学网</a>
            <a href="#">畅学网</a>
            <a href="#">畅学网</a>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
function menuFixed(id){
    var obj = document.getElementById(id);
    var _getHeight = obj.offsetTop;
    
    window.onscroll = function(){
        changePos(id,_getHeight);
    }
}
function changePos(id,height){
    var obj = document.getElementById(id);
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    if(scrollTop < height){
        obj.style.position = 'relative';
    }else{
        obj.style.position = 'fixed';
    }
}
window.onload = function(){
    menuFixed('nav');
}
</script>

 

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 0/1554 人参与

发表评论:

欢迎使用手机扫描访问本站,还可以关注微信哦~