Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**Menu anchor**/
  2.     // Cache selectors
  3.     var lastId,
  4.         topMenu = $(".menu-anchor"),
  5.         windowWidth = $(window).width();
  6.         topMenuHeight = topMenu.outerHeight(),
  7.         // All list items
  8.         menuItems = topMenu.find("a"),
  9.         // Anchors corresponding to menu items
  10.         scrollItems = menuItems.map(function(){
  11.           var item = $($(this).attr("href"));
  12.           if (item.length) { return item; }
  13.         });
  14.  
  15.     // Bind click handler to menu items
  16.     // so we can get a fancy scroll animation
  17.     menuItems.click(function(e) {
  18.         var href = $(this).attr("href"), offsetTop = href === "#" ? 0 : $(href).offset().top;
  19.         $('html, body').stop().animate({
  20.             scrollTop: offsetTop
  21.         }, 1500, 'easeInOutExpo');
  22.         e.preventDefault();
  23.     });
  24.     // Bind to scroll
  25.     var anchorActive = function() {
  26.         var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page
  27.           var windowHeight = $(window).height(); // get the height of the window
  28.           var docHeight = $(document).height();
  29.            // Get container scroll position
  30.            var fromTop = $(this).scrollTop()+40;
  31.            
  32.            // Get id of current scroll item
  33.            var cur = scrollItems.map(function(){
  34.              if ($(this).offset().top < fromTop)
  35.                return this;
  36.            });
  37.            // Get the id of the current element
  38.            cur = cur[cur.length-1];
  39.            var id = cur && cur.length ? cur[0].id : "";
  40.            if(windowPos + windowHeight == docHeight) {
  41.               if (!$(".menu-anchor li:last-child").hasClass("active")) {
  42.                   var navActiveCurrent = $(".menu-anchor .active a").attr("href");
  43.                   $("a[href='" + navActiveCurrent + "']").parent().removeClass("active");
  44.                   $(".menu-anchor li:last-child").addClass("active");
  45.               }
  46.             }
  47.             else {
  48.               lastId = id;
  49.               // Set/remove active class
  50.               menuItems.parent().removeClass("active")
  51.               .end().filter("[href=#"+id+"]").parent().addClass("active");
  52.             }  
  53.     }
  54.     $(window).load(function(){
  55.         anchorActive();
  56.     });
  57.     $(window).scroll(function(){
  58.         anchorActive();
  59.     });
  60.     $( window).resize(function() {
  61.         anchorActive();
  62.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement