简体   繁体   English

在滚动中使用JQuery切换类活动

[英]Toggle Class Active with JQuery On Scroll

I am trying to toggle active class in the nav links while scrolling and here's my code : 我试图在滚动时切换导航链接中的活动类,这是我的代码:

 $(function () { "use strict"; $(window).on("scroll", function (event) { var $scrollPos = $(document).scrollTop(), $links = $('.nav ul li a'); $links.each(function () { var $currLink = $(this), $refElement = $($currLink.attr("href")); if ($refElement.position().top <= $scrollPos && $refElement.position().top + $refElement.height() > $scrollPos) { $links.removeClass("active"); $currLink.addClass("active"); } else { $currLink.removeClass("active"); } }); }); }); 
 .nav { background-color:#222; padding:15px 10px; position:fixed; top:0; left:0; width:100%; } .nav ul li { display:inline; margin:0 20px; } .nav ul li a { text-decoration:none; color:#fff; } .active { color:red; } #home,#about,#services,#contact { height:600px; } h1 { text-align:center; font-size:30px; padding:100px 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="nav"> <ul> <li><a href="#home" class="smoothScroll">Home</a></li> <li><a href="#about" class="smoothScroll">About</a></li> <li><a href="#services" class="smoothScroll">Services</a></li> <li><a href="#contact" class="smoothScroll">Contact</a></li> </ul> </div> <div id="home"> <h1>Home</h1> </div> <div id="about"> <h1>about</h1> </div> <div id="services"> <h1>services</h1> </div> <div id="contact"> <h1>contact</h1> </div> 

But something went wrong and it doesn't work - maybe someone can help me find out where's my wrong. 但是出了点问题,但没有用-也许有人可以帮助我找出我的错在哪里。

Use .nav ul li a.active instead of .active because .nav ul li a is more specific style than .active - see demo below: 使用.nav ul li a.active而不是.active因为.nav ul li a.active具体的样式 -请参见下面的演示:

 $(function () { "use strict"; $(window).on("scroll", function (event) { var $scrollPos = $(document).scrollTop(), $links = $('.nav ul li a'); $links.each(function () { var $currLink = $(this), $refElement = $($currLink.attr("href")); if ($refElement.position().top <= $scrollPos && $refElement.position().top + $refElement.height() > $scrollPos) { $links.removeClass("active"); $currLink.addClass("active"); } else { $currLink.removeClass("active"); } }); }); }); 
 .nav { background-color:#222; padding:15px 10px; position:fixed; top:0; left:0; width:100%; } .nav ul li { display:inline; margin:0 20px; } .nav ul li a { text-decoration:none; color:#fff; } .nav ul li a.active { color:red; } #home,#about,#services,#contact { height:600px; } h1 { text-align:center; font-size:30px; padding:100px 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="nav"> <ul> <li><a href="#home" class="smoothScroll">Home</a></li> <li><a href="#about" class="smoothScroll">About</a></li> <li><a href="#services" class="smoothScroll">Services</a></li> <li><a href="#contact" class="smoothScroll">Contact</a></li> </ul> </div> <div id="home"> <h1>Home</h1> </div> <div id="about"> <h1>about</h1> </div> <div id="services"> <h1>services</h1> </div> <div id="contact"> <h1>contact</h1> </div> 

use this in your css instead of only .active : 在您的CSS中使用它,而不是仅.active

.nav ul li a.active {
    color:red;
}

OR you can use !important like this : 或者您可以使用!important像这样:

.active {
    color:red!important;
}

But be careful, read this subject 但请注意,阅读本主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM