script.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function activenav() {
  2. // Cache selectors
  3. var lastId,
  4. topMenu = $(".navigation"),
  5. topMenuHeight = topMenu.outerHeight()+500,
  6. // All list items
  7. menuItems = topMenu.find("a"),
  8. // Anchors corresponding to menu items
  9. scrollItems = menuItems.map(function(){
  10. var item = $($(this).attr("href"));
  11. if (item.length) { return item; }
  12. });
  13. // Bind click handler to menu items
  14. // so we can get a fancy scroll animation
  15. // Bind to scroll
  16. $(window).scroll(function(){
  17. // Get container scroll position
  18. var fromTop = $(this).scrollTop()+topMenuHeight;
  19. // Get id of current scroll item
  20. var cur = scrollItems.map(function(){
  21. if ($(this).offset().top < fromTop)
  22. return this;
  23. });
  24. // Get the id of the current element
  25. cur = cur[cur.length-1];
  26. var id = cur && cur.length ? cur[0].id : "";
  27. if (lastId !== id) {
  28. lastId = id;
  29. // Set/remove active class
  30. menuItems
  31. .parent().removeClass("active")
  32. .end().filter("[href='#"+id+"']").parent().addClass("active");
  33. }
  34. });
  35. }
  36. jQuery(document).ready(function($) {
  37. activenav();
  38. });