Skip to content

Instantly share code, notes, and snippets.

@charleslouis
Created September 22, 2018 07:47
Embed
What would you like to do?
export function addClassToBodyOnscroll(elementReference) {
let timer;
const mainNav = $(elementReference);
const mainNavH = mainNav.outerHeight();
console.group('scrollTop');
console.log('mainNavH', mainNavH);
console.groupEnd();
$(window).scroll(function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
if (($(window).scrollTop()) < 2) {
$('body').removeClass('scrolled');
} else {
$('body').addClass('scrolled');
}
if (($(window).scrollTop()) < mainNavH) {
$('body').removeClass('scrolled-below-nav');
} else {
$('body').addClass('scrolled-below-nav');
}
}, 10);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment