欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript改變position值實(shí)現(xiàn)菜單滾動(dòng)至頂部后固定

 更新時(shí)間:2013年01月18日 18:22:13   作者:  
現(xiàn)在很多網(wǎng)站都有這樣的一個(gè)效果,當(dāng)頁(yè)面滾動(dòng)到一定高度時(shí),菜單欄會(huì)固定在頁(yè)面頂部;該效果在 ie6 下不支持,因?yàn)閕e6不支持 position:fixed,效果很不錯(cuò),感興趣的朋友可以了解下啊
現(xiàn)在很多網(wǎng)站都有這樣的一個(gè)效果,當(dāng)頁(yè)面滾動(dòng)到一定高度時(shí),菜單欄會(huì)固定在頁(yè)面頂部。其實(shí)就是改變 position 的值。
html 代碼
復(fù)制代碼 代碼如下:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/base.css" media="all"/>
<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="#">11111</a>
<a href="#">22222</a>
<a href="#">33333</a>
<a href="#">44444</a>
<a href="#">55555</a>
</div>
</div>
</body>
</html>
<script type="text/javascript" src="menuFixed.js"></script>
<script type="text/javascript">
window.onload = function(){
menuFixed('nav');
}
</script>

menuFixed.js 代碼
復(fù)制代碼 代碼如下:

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';
}
}

最后需要說明的是,該效果在 ie6 下不支持,因?yàn)?ie6 不支持 position:fixed;
PS:這是本人閑著無聊,通過自己所學(xué)的 javascript 知識(shí),隨意寫的一些效果。

相關(guān)文章

最新評(píng)論