jquery - Change the color of the nav tag when scroll -


i want change color of <a> element of nav when scroll down.

here repo https://github.com/sebalaini/twelfth_project_treehouse.com , here example of take original jquery code https://codyhouse.co/gem/vertical-fixed-navigation/

i change code project doesn't work, what's wrong ?

var contentsections = $('.section'); var navigationitems = $('.nav a');  updatenavigation(); $(window).on('scroll', function(){     updatenavigation(); });  function updatenavigation() {     contentsections.each(function(){         $this = $(this);         var activesection = $('.nav a[href="#'+$this.attr("class")+'"]');         if ( ( $this.offset().top - $(window).height()/2 < $(window).scrolltop() ) && ( $this.offset().top + $this.height() - $(window).height()/2 > $(window).scrolltop() ) ) {             navigationitems.eq(activesection).addclass('selected');         }else {             navigationitems.eq(activesection).removeclass('selected');         }     }); } 

here codepen: http://codepen.io/esten/pen/gjaxrx

there few issues. activesection selector returning of class names on element, not solely section identifier want. changed selector .attr('id') rather class (i had add id portfolio section, , changed id of portfolio header accommodate this).

var contentsections = $('.section'); var navigationitems = $('.nav a');  updatenavigation(); $(window).on('scroll', function() {   updatenavigation(); });  function updatenavigation() {    contentsections.each(function() {     //console.log($(this));     $this = $(this);     var activesection = $('.nav a[href="#' + $this.attr("id") + '"]');      if (($this.offset().top - $(window).height() / 2 < $(window).scrolltop()) && ($this.offset().top + $this.height($(window).height() / 2 >     $(window).scrolltop())) {       activesection.addclass('selected');     } else {       activesection.removeclass('selected');     }   }); } 

also, since activesection selecting anchor tag element, need add class directly element.

this should selected class applied nav elements, need more specific css selector:

.nav a.selected {   color: blue } 

hope helps!


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -