1. 程式人生 > >jQuery網頁向下滾動導航固定頂部代碼

jQuery網頁向下滾動導航固定頂部代碼

頂部 top div lin move c51 att eat oct

技術分享
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery網頁向下滾動導航固定頂部代碼</title>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/index.css" >
</head>  
<body><script src="/demos/googlegg.js"></script>
<div id="nav-wrap" style="height:1900px;background:#FC0;margin-top:40px;position:relative;">
    <ul id="nav" style="height:60px;background:#0F9;top:300px;position:absolute;width:100%;">
        <li>導航欄項目一</li>
        <li>導航欄項目二</li>
        <li>導航欄項目三</li>
        <li>導航欄項目四</li>
        <li>導航欄項目五</li>
    </ul>
</div>

<script>
//
方法一: $(function(){ var a = $(‘#nav‘), b =a.offset(); $(document).on(‘scroll‘,function(){ var c = $(document).scrollTop(); if(b.top<=c){ a.css({‘position‘:‘fixed‘,‘top‘:‘0px‘}) }else{ a.css({‘position‘:‘absolute‘,‘top‘:‘300px‘}) } }) }) //方法二: /*
$(function(){ var a = $(‘#nav‘), b =a.offset(); $(document).on(‘scroll‘,function(){ var c = $(document).scrollTop(); if(b.top<=c){ a.css({‘position‘:‘fixed‘,‘top‘:‘0px‘,‘background‘:‘#fff‘,‘z-index‘:‘3‘,‘border-bottom‘:‘3px solid #ccc‘,‘box-shadow‘:‘2px 2px 2px 2px #555‘}) }else if(b.top>c){ a.removeAttr(‘style‘); } }) })
*/ </script> </body> </html>
View Code

————————————————————

jQuery網頁向下滾動導航固定頂部代碼