zoukankan      html  css  js  c++  java
  • 滚动页面时DIV到达顶部时固定在顶部

    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    .wrapper{1000px;height:2000px;margin-left:auto;margin-right:auto;}
    .header{height:150px;}
    #nav{padding:10px;position:relative;top:0;background:black;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>

    来自:http://xiaohuang.cc/post/284.html

  • 相关阅读:
    近似计算π(for循环)
    apache部署mo_python
    文件注释方法
    算法效率的度量
    ssh
    使用类名创建对象
    筛选网址
    常用django命令
    查看和关闭端口
    update脚本出错
  • 原文地址:https://www.cnblogs.com/lijIT/p/4498732.html
Copyright © 2011-2022 走看看