網頁吸頂
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;}
#header{
width: 100%;
height: 200px;
}
ul,li{list-style: none;}
#nav{
width: 100%;
height: 80px;
background: #000;
}
#nav>ul>li{
float: left;
text-align: center;
line-height: 80px;
color: #fff;
width: 80px;
}
</style>
</head>
<body style="height:3000px;">
<div id="header"></div>
<div id="nav">
<ul>
<li>首頁</li>
<li>文檔</li>
<li>我的</li>
<li>設置</li>
</ul>
</div>
</body>
</html>
<script>
var oNav = document.getElementById("nav");
window.onscroll = function () {
var sTop = document.documentElement.scrollTop || document.body.scrollTop;
if(sTop>=200){
oNav.style.position = "fixed";
oNav.style.top = 0;
}else{
oNav.style.position = "static";
}
}
</script>
網頁吸頂