JS滾動(dòng)到指定位置導(dǎo)航欄固定頂部
最近整理一下之前做的一個(gè)項(xiàng)目,把滾動(dòng)條動(dòng)態(tài)固定頂部的代碼整理出來(lái)和大家分享,上代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js滾動(dòng)到指定位置導(dǎo)航欄固定頂部</title> <style type="text/css"> body{height: 2500px; margin: 0; padding: 0;} .banner{height: 250px; width: 100%; background: #e5e5e5;} .bignav{width: 100%; background: #000;} .nav{ background:#000; width: 1200px; margin: 0 auto; height: 45px;} .nav a{display: block; width: 200px;float: left; color: #fff; text-decoration: none; text-align: center; line-height: 45px;} </style> </head> <body> <div class="banner"> </div> <div class="bignav" id="bignav"> <div class="nav"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> </div> </div> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <p>其他內(nèi)容</p> <script type="text/javascript"> window.onscroll=function(){ var topScroll =document.body.scrollTop;//滾動(dòng)的距離,距離頂部的距離 var bignav = document.getElementById("bignav");//獲取到導(dǎo)航欄id if(topScroll > 250){ //當(dāng)滾動(dòng)距離大于250px時(shí)執(zhí)行下面的東西 bignav.style.position = 'fixed'; bignav.style.top = '0'; bignav.style.zIndex = '9999'; }else{//當(dāng)滾動(dòng)距離小于250的時(shí)候執(zhí)行下面的內(nèi)容,也就是讓導(dǎo)航欄恢復(fù)原狀 bignav.style.position = 'static'; } } </script> </body> </html>
以上所述是小編給大家介紹的JS滾動(dòng)到指定位置導(dǎo)航欄固定頂部,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
security.js實(shí)現(xiàn)的RSA加密功能示例
這篇文章主要介紹了security.js實(shí)現(xiàn)的RSA加密功能,結(jié)合實(shí)例形式分析了基于security.js進(jìn)行RSA加密的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06EasyUi中的Combogrid 實(shí)現(xiàn)分頁(yè)和動(dòng)態(tài)搜索遠(yuǎn)程數(shù)據(jù)
jquery easyui中的combogrid比較特殊,算是combo和grid的組合,combogrid結(jié)合一個(gè)可編輯的文本框和下拉數(shù)據(jù)網(wǎng)格面板,可以讓用戶迅速找到并選擇,又可以進(jìn)行搜索,展示與當(dāng)前輸入的字符相匹配的數(shù)據(jù)。下面給大家介紹EasyUi中的Combogrid 實(shí)現(xiàn)分頁(yè)和動(dòng)態(tài)搜索遠(yuǎn)程數(shù)據(jù)2016-04-04jQuery實(shí)現(xiàn)手風(fēng)琴特效
這篇文章主要為大家詳細(xì)介紹了前端js實(shí)現(xiàn)手風(fēng)琴效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01JavaScript實(shí)現(xiàn)的商品搶購(gòu)倒計(jì)時(shí)功能示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的商品搶購(gòu)倒計(jì)時(shí)功能,可實(shí)現(xiàn)分秒級(jí)別的實(shí)時(shí)顯示倒計(jì)時(shí)效果,涉及js日期時(shí)間計(jì)算與頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2017-04-04js實(shí)現(xiàn)數(shù)組轉(zhuǎn)樹示例
這篇文章主要為大家介紹了js實(shí)現(xiàn)數(shù)組轉(zhuǎn)樹示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06JavaScript中break、continue和return的用法區(qū)別實(shí)例分析
這篇文章主要介紹了JavaScript中break、continue和return的用法區(qū)別,結(jié)合實(shí)例形式詳細(xì)對(duì)比分析了JavaScript中break、continue和return的基本功能、使用方法、區(qū)別與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03