Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
onScroll Header animation

onScroll Header animation

// Header Styles
var stuck = false;
var stickPoint = getDistance();
function getDistance() {
var topDist = topNav.offsetTop;
return topDist;
}
window.onscroll = function(e) {
var distance = getDistance() - window.pageYOffset;
var offset = window.pageYOffset;
if ( (distance <= 0) && !stuck) {
topNav.classList.add('top');
stuck = true;
} else if (stuck && (offset <= stickPoint)){
topNav.classList.remove('top');
stuck = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment