Advertisement
firoze

menu scrolling and activation for id

Apr 14th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. // menu scrolling and activation for id
  2. var lastId,
  3. topMenu = jQuery("#top-navigation"), // here #top-navigation is id of <ul id="top-navigation"></ul>
  4. topMenuHeight = topMenu.outerHeight()+15,
  5. menuItems = topMenu.find('a[href^="#"]'),
  6. scrollItems = menuItems.map(function(){
  7. var item = jQuery($(this).attr("href"));
  8. if (item.length) { return item; }
  9. });
  10. menuItems.click(function(e){
  11. var href = jQuery(this).attr("href"),
  12. offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
  13. jQuery('html, body').stop().animate({
  14. scrollTop: offsetTop
  15. }, 300);
  16. e.preventDefault();
  17. });
  18. jQuery(window).scroll(function(){
  19. var fromTop = $(this).scrollTop()+topMenuHeight;
  20. var cur = scrollItems.map(function(){
  21. if ($(this).offset().top < fromTop)
  22. return this;
  23. });
  24. cur = cur[cur.length-1];
  25. var id = cur && cur.length ? cur[0].id : "";
  26. if (lastId !== id) {
  27. lastId = id;
  28. menuItems
  29. .parent().removeClass("active")
  30. .end().filter("[href=#"+id+"]").parent().addClass("active");
  31. }
  32. });
  33.  
  34.  
  35.  
  36.  
  37. // for wordpress memu
  38.  
  39. <?php wp_nav_menu( array( 'theme_location' => 'plutin_menu_blog','menu_id' => 'top-navigation', ) ); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement