您的位置:首页 > Web前端 > JavaScript

js 悬浮导航

2016-05-05 19:18 369 查看
<head>
<meta charset="UTF-8">
<title>悬浮导航</title>
<style>
* {
margin: 0px;
padding: 0px;
}
ul li{
list-style: none;
}
body{
height: 2000px;
}
#top{
height: 300px;
width: 100%;
background-color: red;
}
#nav{
height: 50px;
line-height: 50px;
width: 100%;
background-color: pink;
}
#nav ul{
width: 1200px;
margin: 0 auto;
}
#nav ul li{
float: left;
width: 164px;
text-align: center;
}
</style>
</head>
<body>
<div id="top">
顶部
</div>
<div id="nav">
<ul>
<li>首页</li>
<li>首页</li>
<li>首页</li>
<li>首页</li>
<li>首页</li>
<li>首页</li>
<li>首页</li>
</ul>
</div>
</body>
<script>
var ad = document.getElementById("nav")
window.onscroll = function(){
var _top = document.body.scrollTop || document.documentElement.scrollTop;//兼容
if(_top>=300){
ad.style.position = "fixed";
ad.style.top = 0 +"px";
}else{
ad.style.position = "absolute";
ad.style.top = 300+"px";
}
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript 导航 悬浮