2016-12-19 67 views
1

我试图切换主动类的导航链接,同时滚动,这里是我的代码:切换类主动与jQuery在滚动

$(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>

但出了问题,它不工作 - 也许有人可以帮我找出我的错在哪里。

回答

1

使用这个在你的CSS,而不是只.active

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

或者您可以使用!important这样的:

.active { 
    color:red!important; 
} 

但是要小心,阅读本subject

+0

问题并不在CSS。我已经在真正的项目中这样做了,但也有同样的问题。 –

+0

这不是你所期望的? https://jsfiddle.net/v34nkwrp/ –

+3

是的,你是对的,它现在的作品:) –

3

使用.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>

+2

谢谢你的作品。 –