jquery - Active menu Changer on Sticky Bar -


i have designed parallax page sticky menu bar. need change active menu on scrolling. have made change active class on click event. need scroll event.

here html code

<div class="main-menu">      <ul>                      <li><a class="active" href="#" data-delay="2000" data-appear="false" data-scrollto="#intro-slideshow">home</a></li>         <li><a href="#" data-delay="2000" data-appear="false" data-scrollto="#overview">features</a></li>         <li><a href="#" data-delay="2000" data-appear="false" data-scrollto="#categories">categories</a></li>         <li><a href="#" data-delay="2000" data-appear="false" data-scrollto="#contact">contact us</a></li>     </ul>  </div> 

here jquery code onclick active menu changer

$('*[data-scrollto]').click(function(){          $( "a" ).removeclass( "active" );         $(this).addclass("active");          var dest = $(this).data('scrollto');                         var pixels = $(dest).offset().top - 70;                  var ms = math.round(1000 + pixels/5);          $('html, body').animate({                            scrolltop: pixels         }, ms, 'easeoutquint');      });  

how change active class on scroll event data-scrollto attribute ?

without going loads of detail. need bind scroll event containing element or window.

http://api.jquery.com/scroll/

here's demo of similar. hope helps.

https://jsfiddle.net/vsmrnd7l/1/

edit:

var lastid,     topmenu = $("#top-menu"),     topmenuheight = topmenu.outerheight()+15,     menuitems = topmenu.find("a"),     scrollitems = menuitems.map(function(){       var item = $($(this).attr("href"));       if (item.length) { return item; }     });  menuitems.click(function(e){   var href = $(this).attr("href"),       offsettop = href === "#" ? 0 : $(href).offset().top-topmenuheight+1;   $('html, body').stop().animate({        scrolltop: offsettop   }, 300);   e.preventdefault(); });  $(window).scroll(function(){    var fromtop = $(this).scrolltop()+topmenuheight;    var cur = scrollitems.map(function(){      if ($(this).offset().top < fromtop)        return this;    });     cur = cur[cur.length-1];    var id = cur && cur.length ? cur[0].id : "";     if (lastid !== id) {        lastid = id;        menuitems          .parent().removeclass("active")          .end().filter("[href=#"+id+"]").parent().addclass("active");    }                    }); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -