垂直滾動條滑到頁面里面的導航欄時,導航欄固定到窗口頂部


如標題,這個功能,有很大的實用性,通過樣式以及JS,還是比較容易實現的。

不多說了,直接上代碼:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>菜單導航可懸浮在頂部</title>
 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">    
 6     <script type="text/javascript" src="js/jquery-3.1.0.js"></script>
 7 </head>
 8 <body>
 9     <style type="text/css">
10         * {
11             padding:0;
12             margin: 0;
13         }        
14         .nav {border:1px solid green; position:relative;height:50px;}
15         .nav ul{list-style-type:none;height:50px;background:gray;}
16         .nav ul li{float:left;width:158px;text-align:center;height:50px;line-height:50px;}
17         .nav ul li a:link{color:white;text-decoration:none;}
18         .nav ul li a{display:block;}
19         .nav ul li a:hover{background:#f60;}
20         .nav_fix_pos {
21             position:fixed;top:0px;left:0px;width:100%;
22             background: blue;
23         }
24     </style>
25     <div style="margin:10px 150px 10px 150px; color: #00ff99;">
26         <h2>
27         <br/>開發流程
28         <br/>
29         <br/>1)獲取用戶授權(這個做不做沒有啥關系,反正我還沒做呢)
30         <br/>2)調用統一下單接口獲取預支付id
31         <br/>3)H5調起微信支付的內置JS
32         <br/>4)支付完成后,微信回調URL的處理
33         </h2>
34         <div style="margin-top: 300px;"></div>        
35     </div>
36     <div class="clearfix">
37         <div class="nav">
38             <ul>
39                 <li><a id=".product_menu1" style="cursor: pointer;">產品介紹</a></li>
40                 <li><a id="#idcase" style="cursor: pointer;">保險案例</a></li>
41                 <li><a id="#idclaim" style="cursor: pointer;">理賠服務</a></li>
42                 <li><a id="#idfaq" style="cursor: pointer;">常見問題</a></li>
43                 <li><a id="#idappraise" style="cursor: pointer;">累計評價(<span id="commentAllCount">23</span>條)</a></li>
44                 <li><div id="ljgm" class="ljgm" onclick="buyFunc()" style="display: block; z-index: 1000; top: 0px;"><a id="ljgma" href="#">測算保費</a></div></li>
45             </ul>          
46         </div>
47         <div style="margin-top: 300px; clear: both;"></div>    
48     </div>
49     <div style="margin:10px 150px 10px 150px; color: #1100ff;">        
50         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
51         <div style="margin-top: 300px;"></div>
52         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
53         <div style="margin-top: 300px;"></div>
54         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
55         <div style="margin-top: 300px;"></div>
56         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
57         <div style="margin-top: 300px;"></div>
58         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
59         <div style="margin-top: 300px;"></div>
60         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
61         <div style="margin-top: 300px;"></div>
62         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
63         <div style="margin-top: 300px;"></div>
64         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
65         <div style="margin-top: 300px;"></div>
66         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
67         <div style="margin-top: 300px;"></div>
68         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
69         <div style="margin-top: 300px;"></div>
70         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
71         <div style="margin-top: 300px;"></div>
72         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
73         <div style="margin-top: 300px;"></div>
74         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
75         <div style="margin-top: 300px;"></div>
76         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
77         <div style="margin-top: 300px;"></div>
78         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
79         <div style="margin-top: 300px;"></div>
80         <h2>繼續看下面啊。。。。。。。。。。。。</h2>
81         <div style="margin-top: 300px;"></div>
82     </div>
83     <script>
84         $(document).ready(function(){
85             var nav=$(".nav"); //得到導航對象
86             var win=$(window); //得到窗口對象
87             var sc=$(document);//得到document文檔對象。
88             var navHeight = nav.offset().top;            
89             win.scroll(function(){                            
90               if(sc.scrollTop() >= navHeight){                                    
91                     nav.addClass("nav_fix_pos");
92               }else{
93                     nav.removeClass("nav_fix_pos");                    
94               };
95             });
96         });
97     </script>
98 </body>
99 </html>

效果圖如下,先看鼠標沒有拖到導航欄所在的位置時:

再來看看,滑動條拖到超過導航欄所在的位置時的效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM