我将一个滚动事件监听器附加到包装 div,然后对于具有“js-chart”类的每个 div,它将检查该特定 js-chart 是否在 View 内,并将在其上执行函数。然后,它将从该元素中删除 js-chart 类,以便它不会继续触发该函数。
这一切都工作正常,但我想做的是,当所有 js-charts 都被触发时(因此 js-chart 中不再有元素),要完全删除滚动事件监听器,所以我我不会一直进行毫无意义的检查。
$('#wrapper').scroll(function() {
var $wrapper = $(this);
$('.js-chart').each( function (n) {
var $this = $(this);
if ( $this.position().top + $this.height() > 100 && $this.position().top < $wrapper.height() ) {
console.log("Test");
createChart($this);
$this.removeClass('js-chart');
}
});
});
最佳答案
添加如下内容:
$('#wrapper').on("scroll", function() {
var $wrapper = $(this);
$('.js-chart').each( function (n) {
var $this = $(this);
if ( $this.position().top + $this.height() > 100 && $this.position().top < $wrapper.height() ) {
console.log("Test");
createChart($this);
$this.removeClass('js-chart');
}
});
if (!$('.js-chart').length) {
$wrapper.off("scroll");
}
});
关于javascript - 函数完成后如何删除滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28550887/