關(guān)于使用jquery導(dǎo)航跟隨置頂?shù)牟寮谥耙呀?jīng)分享過幾個了:
1、jQuery 導(dǎo)航菜單、廣告 —— 固定、置頂、跟隨
2、jQuery 智能判斷跟隨頁面滾動的導(dǎo)航、菜單、廣告,下拉后跟隨置頂特效代碼
3、jQuery - 多個菜單導(dǎo)航滾動跟隨,全部積累固定在頂端
4、jquery 滾動跟隨 到達(dá)底部時會停止跟隨特效代碼
而今天要分享的,是帶錨點定位自動跟隨!
這是一個插件,所以JS:nav.zip
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>導(dǎo)航滾動到一定高底后吸頂,錨點定位自動跟隨</title> <style type="text/css"> *{margin: 0; padding: 0;} .topbox{ height: 500px; background-color: #313131; } .section-content{ border-bottom: 5px solid #0000FF; min-height: 500px; background: #eee; width: 1200px; margin: auto; line-height: 500px; text-align: center; } .nav-height{ position:relative; height: 50px;} .nav-wrap { width: 100%;height: 50px;background: #fff;} .nav-wrap.navFix{position: fixed;top: 0;left: 0; box-shadow: 0 0 5px rgba(0,0,0, 0.2);border-bottom:1px solid #e3e3e3\9; z-index: 99999;} .nav-wrap ul {padding: 0;margin: 0 auto; width: 1200px; display: block; border-bottom: 2px solid #eee;} .nav-wrap.navFix ul{ border-bottom: 0;} .nav-wrap li { display: inline-block; text-align: left;height: 50px; line-height: 50px; float: left;} .nav-wrap li a {display: block;padding: 0 20px;font-size:20px; color: #333; text-decoration: none;} .nav-wrap li a:hover{color: #4680d1;} .nav-wrap li a.active {border-bottom:2px solid #4680d1;color: #4680d1;} .nav-mobile {display: none;font-weight: bold;width: 100%;} .click-me {width: 150px;height: 30px; background: #4680d1;color: white; text-align: center;line-height: 30px;} .click-me a {display: block;color: white;} </style> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src='nav.js'></script><!----------文章中提供了JS,請下載使用------------> </head> <body> <div class="topbox"></div> <div class="nav-height" id="navHeight"> <nav class="nav-wrap" id="nav-wrap"> <div class="nav-mobile">Click</div> <ul class="clearfix"> <li><a class="active" href="#section1">nav1</a></li> <li><a class="" href="#section2">nav2</a></li> <li><a class="" href="#section3">nav3</a></li> <li><a class="" href="#section4">nav4</a></li> </ul> </nav> </div> <div id="section1" class="section-content"> 1 </div> <div id="section2" class="section-content"> 2 </div> <div id="section3" class="section-content"> 3 </div> <div id="section4" class="section-content"> 4 </div> <div style="height: 1000px;background: #f5f5f5;">footer</div> <!--內(nèi)容信息導(dǎo)航吸頂及錨點引入JS--> <script> //內(nèi)容信息導(dǎo)航吸頂 $(document).ready(function(){ var navHeight= $("#navHeight").offset().top; var navFix=$("#nav-wrap"); $(window).scroll(function(){ if($(this).scrollTop()>navHeight){ navFix.addClass("navFix"); } else{ navFix.removeClass("navFix"); } }) }) //內(nèi)容信息導(dǎo)航錨點 $('.nav-wrap').navScroll({ mobileDropdown: true, mobileBreakpoint: 768, scrollSpy: true }); $('.click-me').navScroll({ navHeight: 0 }); $('.nav-wrap').on('click', '.nav-mobile', function (e) { e.preventDefault(); $('.nav-wrap ul').slideToggle('fast'); }); </script> </body> </html>