Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Menu anchor**/
- // Cache selectors
- var lastId,
- topMenu = $(".menu-anchor"),
- windowWidth = $(window).width();
- topMenuHeight = topMenu.outerHeight(),
- // All list items
- menuItems = topMenu.find("a"),
- // Anchors corresponding to menu items
- scrollItems = menuItems.map(function(){
- var item = $($(this).attr("href"));
- if (item.length) { return item; }
- });
- // Bind click handler to menu items
- // so we can get a fancy scroll animation
- menuItems.click(function(e) {
- var href = $(this).attr("href"), offsetTop = href === "#" ? 0 : $(href).offset().top;
- $('html, body').stop().animate({
- scrollTop: offsetTop
- }, 1500, 'easeInOutExpo');
- e.preventDefault();
- });
- // Bind to scroll
- var anchorActive = function() {
- var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page
- var windowHeight = $(window).height(); // get the height of the window
- var docHeight = $(document).height();
- // Get container scroll position
- var fromTop = $(this).scrollTop()+40;
- // Get id of current scroll item
- var cur = scrollItems.map(function(){
- if ($(this).offset().top < fromTop)
- return this;
- });
- // Get the id of the current element
- cur = cur[cur.length-1];
- var id = cur && cur.length ? cur[0].id : "";
- if(windowPos + windowHeight == docHeight) {
- if (!$(".menu-anchor li:last-child").hasClass("active")) {
- var navActiveCurrent = $(".menu-anchor .active a").attr("href");
- $("a[href='" + navActiveCurrent + "']").parent().removeClass("active");
- $(".menu-anchor li:last-child").addClass("active");
- }
- }
- else {
- lastId = id;
- // Set/remove active class
- menuItems.parent().removeClass("active")
- .end().filter("[href=#"+id+"]").parent().addClass("active");
- }
- }
- $(window).load(function(){
- anchorActive();
- });
- $(window).scroll(function(){
- anchorActive();
- });
- $( window).resize(function() {
- anchorActive();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement