您好,登錄后才能下訂單哦!
本文實例講述了jQuery實現的監聽導航滾動置頂狀態功能。分享給大家供大家參考,具體如下:
1. js代碼
/** * Created by EDUASK on 2016/5/20. */ $(function(){ //引入id標簽; var navtive=$("#native"); //設置導航標簽為置頂; var offsetTop=navtive.offset().top; //定義一個監聽高度; var scrollHeight=$(document).scrollTop; fn(navtive,offsetTop); //設置監聽事件; $(window).scroll(function(){ fn(navtive,offsetTop); }); //調用方法; function fn(navtive,offsetTop){ var scrollHeight=$(document).scrollTop(); if(offsetTop>scrollHeight){ navtive.css("top",offsetTop); }else{ navtive.css("top",scrollHeight); } } });
2. html代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>監聽導航滾動置頂</title> <!--css樣式;--> <style type="text/css"> /*添加導航條的樣式; */ #native{ background: blue; width: 100%; height: 20px; color: red; position: absolute; top: 100px; left: 0; text-align: center; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript" src="監聽導航滾動置頂.js"> </script> </head> <body> <p id="top">ddddd</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <div id="native">歡迎訪問我的導航條</div> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <a href="#top" rel="external nofollow" >回到頂部</a> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> <p>aaaaaaaaaaaa</p> </body> </html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試運行效果。
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結》、《jQuery常用插件及用法總結》、《jQuery操作json數據技巧匯總》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。