简单的scrollspy效果jquery

时间:2016-07-27 08:27:50

标签: javascript jquery scrollspy

提前抱歉,初学者提问: - )

我正试图在导航上实现滚动效果。基本上,当我滚动到相应的部分时,我只需要我的链接变成红色。 我已经看过并在网上找到了一些例子,但我没有尝试过,我的JS无论如何都很邋。

有人可以帮到这里吗?

这是我到目前为止的完整JSFiddle: https://jsfiddle.net/Tiph/v6vtczwe/

非常感谢你的时间和帮助!

    $(document).ready(function(){


    //SMOOTHSCROLL
    $('.nav-top a, .scrollDown').click(function(){  
      $('html, body').animate({
        scrollTop: $( $(this).attr('href') )
        .offset().top 
      }, 700);
      return false;
    });

});

$(window).scroll(function(){ 
    var $window =$(window);
  var scroll_top = $(window).scrollTop();
   console.log( $(window).scrollTop());
  var position = $("section").offset().top;
  var news = $("#newsSection").offset().top;
    var shows = $("#showsSection").offset().top;

if (scroll_top >= news) {
             $('.news').addClass("selected");
             }
if (scroll_top >= shows) {
             $('.shows').addClass("selected");
             }

});

1 个答案:

答案 0 :(得分:1)



$('.nav-top a, .scrollDown').click(function() {
  $('html, body').animate({
    scrollTop: $($(this).attr('href'))
      .offset().top
  }, 700);
  return false;
});

$(window).scroll(function() {
  var x = $(".nav-top").offset().top;
  $("section").each(function(index) {
    var z = $(this).attr("id");
    if (x > $(this).offset().top && x <= $(this).offset().top + $(this).height()) {
      $('a.' + z).css("color", "red");
    } else {
      $('a.' + z).css("color", "gray")
    }
  })
})
&#13;
.menu > nav {
  height: 50px;
  position: fixed;
  bottom: 0%;
  width: 100%;
  background-color: #393838;
  opacity: 1;
  padding-left: 70px;
}
.selected {
  color: red
}
.nav-top {
  padding: 15px 0;
  background: rgb(034, 034, 034);
  position: relative;
  z-index: 900;
  display: flex;
  flex-flow: row wrap;
  position: relative;
}
.nav-top a {
  color: white;
  text-decoration: none;
  text-transform: uppercase;
  font-family: 'Oswald';
  font-size: 30px;
  letter-spacing: 2px;
  line-height: 55px;
  margin-right: 30px;
  transition: .6s all ease-in-out;
}
.nav-top a:hover {
  color: #de031d;
  transition: .3s all ease-in-out;
}
.section {
  height: 600px;
}
.grey {
  background-color: grey;
}
.darkGrey {
  background-color: darkgrey;
}
a.newsSection{
color:red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menu">
  <nav class="nav-top">
    <a class="newsSection" href="#newsSection">News</a>
    <a class="showsSection" href="#showsSection">Shows</a>
    <a class="musicSection" href="#musicSection">Discographie</a>
    <a class="mediaSection" href="#mediaSection">Medias</a>
    <a class="bioSection" href="#bioSection">Bio</a>
  </nav>
</div>
<section id="newsSection" class="section grey">
  first section
</section>
<section id="showsSection" class="section darkGrey">
  second section
</section>
<section id="musicSection" class="section grey">
  third section
</section>
<section id="mediaSection" class="section darkGrey">
  fourth section
</section>
<section id="bioSection" class="section grey">
  fifth section
</section>
&#13;
&#13;
&#13;