簡體   English   中英

如何更改滾動導航欄的活動 class

[英]How to change active class of the navbar on scroll

我嘗試了每個我只能找到的js代碼,但沒有任何效果,我不明白為什么,我檢查了控制台,那里沒有錯誤。 也許有人可以發現錯誤? 也許我必須導入一些文件或其他東西? 不知何故,只有當我單擊鏈接時,代碼才看到滾動。 有沒有辦法只能通過 Bootstrap 來做到這一點? 不使用JS?

 $(document).ready(function () { $(document).on("scroll", onScroll); //smoothscroll $('a[href^="#"]').on('click', function (e) { e.preventDefault(); $(document).off("scroll"); $('a').each(function () { $(this).removeClass('active'); }) $(this).addClass('active'); var target = this.hash, menu = target; $target = $(target); $('html, body').stop().animate({ 'scrollTop': $target.offset().top+2 }, 500, 'swing', function () { window.location.hash = target; $(document).on("scroll", onScroll); }); }); }); function onScroll(event){ var scrollPos = $(document).scrollTop(); $('#navbarResponsive a').each(function () { var currLink = $(this); var refElement = $(currLink.attr("href")); if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { $('#navbarResponsive ul li a').removeClass("active"); currLink.addClass("active"); } else{ currLink.removeClass("active"); } }); }
 a:hover, .active a{ border-bottom: 2px solid; font-weight: bolder; border-bottom: 2px solid #0F0F6A; }
 <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> type="image/ico"> <link rel="stylesheet" href="assets/css/styles.css" /> <script src="assets/js/sweetalert.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="assets/script.js"></script> <script src="https://smtpjs.com/v3/smtp.js"></script> <script src='assets/js/bootstrap.min.js'></script> <link rel="stylesheet" href="assets/css/bootstrap.min.css"/> </head> <body> <header> <nav class="navbar navbar-expand-lg xl_padding py-4 py-md-4 navbar-light fixed-top shadow p-3 mb-5 bg-white rounded" id='TopNav'> <div class="container-fluid"> <button class="navbar-toggler ml-auto" type="button" data-toggle="collapse" data-target="#navbarResponsive"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav nav m-auto"> <li class="nav-item first active"> <a class="nav-link gotham" href="#about">About us <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link gotham" href="#calendar">Calendar</a> </li> <li class="nav-item"> <a class="nav-link gotham" href="#feedback">Feedback</a> </li> <li class="nav-item"> <a class="nav-link gotham" href="#photo">Photo</a> </li> </ul> </div> </div> </nav> </header> <section id='about'></section> <section id='calendar'></section> <section id='feedback'></section> <section id='photo'></section> </body>

我已更改 jQuery 腳本並在以下鏈接中更新,請使用以下鏈接獲取示例代碼和 output: https://www.king-subash.com/the-classnavto-change-滾動

無法在此處粘貼整個腳本,因此粘貼在單獨的鏈接中。 在滾動事件中,您可以在同一頁面上查看示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM