DIV滚动到顶部固定在顶部

本文由 Andre tian 于 2014-11-26 5:11 Wednesday 发布在 网页制作 评论(0)

<!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>

 

你想建一个跟《易玩稀有》类似的网站吗?

发表评论:

   

    验证码

支持Ctrl+Enter提交

关于本站  |  畅学网  |  手机版  |  空间支持  |  RSS信息  |  趣味OS  |  QQ交流  |  版权声明 | 豫ICP备 10211463号-1

All Rights Reserved. Powered by Emlog & Themes by ewCEO.com  
当前模板可售并承接各种定制服务
回到顶部