perfect-scrollbar plugin在到达底部时,会多次触发事件

时间:2017-08-10 11:14:01

标签: javascript javascript-events perfect-scrollbar

我正在使用完美滚动插件

https://github.com/noraesae/perfect-scrollbar

当我使用ps-y-reach-end事件

enter image description here

document.addEventListener('ps-y-reach-end', (event)=> { console.log('Why this is printing multiple times when I reach Bottom, I wanted it to be single fire') });

问题是当滚动到达容器底部时,事件会多次触发。

JSFiddle Demo

Web控制台打印:  enter image description here

请将y轴滚动到底部,您将多次看到控制台

1 个答案:

答案 0 :(得分:0)

您需要使用布尔标志来处理此问题

var el = document.querySelector('.container');

Ps.initialize(el);
let loading = false
document.addEventListener('ps-y-reach-end', (event) => {
    if (!loading) {
        loading = true
        alert('Why this is printing multiple times when I reach Bottom, I wanted it to be single fire')
    }

});