DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 菜單滾動至頂部後固定
菜單滾動至頂部後固定
編輯:關於JavaScript     
本實例使用Javascript實現了菜單滾動至頂部後固定的效果,比上個示例效果更完善,上個示例網址http:///a/bjac/ed5eb8c2959c619e.htm

當前實例的效果:http:///keleyi/phtml/fixmenu.htm

以下是源代碼:

<!DOCTYPE HTML>
<html>
<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_keleyi_com{padding:10px;position:relative;top:0;background:#125430;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_keleyi_com">
<a href="http:///menu/net/" target="_blank">.NET</a>
<a href="http:///menu/javascript/" target="_blank">Javascript</a>
<a href="http:///menu/jquery/" target="_blank">jQuery</a>
<a href="http:///menu/csharp/" target="_blank">C#</a>
<a href="http:///menu/other/" target="_blank">其他</a>
<a href="http:///" target="_blank">首頁</a><a href="/menu/aspnet/"

target="_blank">ASP.NET</a> <a href="/menu/mvc/" target="_blank">MVC</a></div>
</div>
<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';
}
}
</script>
<script type="text/javascript">
window.onload = function(){
menuFixed('nav_keleyi_com');
}
</script>
</body>
</html>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved