1

我有一个函数,只有在用户向左或向右滚动时才会触发,方法是执行以下操作:

var currentScrollLeft = visibleArea.scrollLeft();

visibleArea.scroll(function () {
    var newScrollLeft = visibleArea.scrollLeft();

    if (newScrollLeft > currentScrollLeft) {
        // do something
    }
});

但这会在scrollLeft. 有没有办法告诉它只有在移动超过一定数量时才开火,比如说50

4

1 回答 1

3
if((Math.abs(newScrollLeft - currentScrollLeft)) > 50)

所以你上面的代码会变成:

var currentScrollLeft = visibleArea.scrollLeft();

visibleArea.scroll(function () {
    var newScrollLeft = visibleArea.scrollLeft();

    if (Math.abs(newScrollLeft - currentScrollLeft) > 50) {
        // do something
    }

    currentScrollLeft = visibleArea.scrollLeft();
});
于 2012-09-24T12:17:56.547 回答