I have some sample code where I am trying to make a nav sticky when it reaches a certain point. The issue is that the scroll method keeps firing and it appears to be stuck in some kind of loop where the class is being constantly added and removed, making the sticky nave to flickr. Hence my calculations are as follows:
Stickme {
constructor(cfg) {
this.win = $(window);
this.nav = $('.nav');
this.setListeners();
}
setListeners () {
this.win.scroll (() => {
this.debouncedScrollPage = debounce(this.scrollme, 3000);
this.debouncedScrollPage();
});
}
scrollme() {
let total = this.nav.offset().top
if ((total) < this.win.scrollTop()) {
this.nav.addClass('nav--sticky');
}else {
this.nav.removeClass('nav--sticky');
}
}
}