Created
January 8, 2016 01:27
-
-
Save taniarascia/687ff766e5c1325f6ab7 to your computer and use it in GitHub Desktop.
Active Scroll Highlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
$(document).on("scroll", onScroll); | |
// Smooth scroll | |
$('a[href^="#"]').on('click', function (e) { | |
e.preventDefault(); | |
$(document).off("scroll"); | |
$('a').each(function () { | |
$(this).removeClass('active'); | |
}) | |
$(this).addClass('active'); | |
var target = this.hash, | |
menu = target; | |
$target = $(target); | |
$('html, body').stop().animate({ | |
'scrollTop': $target.offset().top + 2 | |
}, 300, 'swing', function () { | |
window.location.hash = target; | |
$(document).on("scroll", onScroll); | |
}); | |
}); | |
}); | |
function onScroll(event) { | |
var scrollPos = $(document).scrollTop(); | |
$('aside a').each(function () { | |
var currLink = $(this); | |
var refElement = $(currLink.attr("href")); | |
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { | |
$('aside nav ul li a').removeClass("active"); | |
currLink.addClass("active"); | |
} else { | |
currLink.removeClass("active"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment