我有一些示例代码,当我到达某个点时,我试图使导航粘。问题是滚动方法一直在触发,它似乎停留在某种循环中,在这种循环中不断添加和删除类,使粘性中心变为flickr。因此我的计算如下:
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');
}
}
}