錨點點擊導航 跳轉到相應位置,樣式隨之變化


// 錨點點擊導航導航樣式變化
			$("#nav-box li a").click(function () {
			    $("html, body").animate({scrollTop: $($(this).attr("href")).offset().top -20+ "px"}, 500);
				$("#nav-box li").each(function(){
					$this = $(this);
					if($this[0].href==String(window.location)){
						$("#nav-box li").removeClass("on");
						$this.addClass("on");
					}
				});
				return false;//不要這句會有點卡頓
			});

  

<div class="nav" id="nav-container">
			<ul class="menu" id="nav-box">`
				<li class="on"><a href="#section1">text1-nav</a></li>
				<li><a href="#section2">text2-nav</a></li>
				<li><a href="#section3">text3-nav</a></li>
			</ul>
		</div>
		<div id="text">
			<div id="section1" style="width:100%;height:500px;background:green;text-align:center;">text1</div>
			<div id="section2" style="width:100%;height:500px;background:yellow;text-align:center;">text2</div>
			<div id="section3" style="width:100%;height:500px;background:blue;text-align:center;">text3</div>
		</div>

  


免責聲明!

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



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