我使用以下iScroll 5代码(一般来说,不是那么重要:只是一个常见的逐页滚动):
var myScroll = new IScroll('.scroller', {
mouseWheel: true,
scrollbars: true,
keyBindings: {
// doesn't matter
},
preventDefault: false,
fadeScrollbars: true,
snap: 'section', // <-- that's the key
wheelAction: 'scroll',
});
myScroll.on('beforeScrollStart', function (e) {
myScroll.preventDisabling = true;
});
myScroll.on('scrollMove', function (e) {
});
myScroll.on('scrollStart', function (e) {
// !!! I need the detection somewhere here !!!
if (!myScroll.preventDisabling) {
myScroll.disable();
disabledWasCalledInMeanwhile = true;
}
myScroll.preventDisabling = false;
});
var disabledWasCalledInMeanwhile = false;
// that's just to prevent jumping to another page before scrolling is finished
myScroll.on('scrollEnd', function (e) {
disabledWasCalledInMeanwhile = false;
window.setTimeout(function () {
if (!disabledWasCalledInMeanwhile)
myScroll.enable();
}, 250);
$('.labels>*').toggleClass('active', false)
.eq(this.currentPage.pageY).toggleClass('active', true);
});
myScroll.on('scrollCancel', function (e) {
myScroll.enable();
});
那么,是否有机会检测beforeScrollStart
或
scrollStart
页面中的我要滚动到?了解这一点对于触发页面项目动画非常重要。
我已经使用 iScroll 很多年了(它是一个优秀的库),但我不知道有什么内置方法可以做到这一点。 iScroll snap 确定之前的所有滚动事件(scrollEnd 除外)。不过,只要对库稍作修改,我相信这是可能的。
首先,进入 iScroll.js 源代码并找到 _nearestSnap 方法。在该方法的底部,您将找到您要返回的对象。在返回之前,获取该数据并将其传递给自定义事件。不幸的是,iScroll 的事件系统不允许您将自定义变量传递给事件,因此您必须采取解决方法。此外,您需要跟踪“flick”事件,因为它不会触发 _nearestSnap 方法。
_nearestSnap 方法中的 iScroll 修改
this.customSnap({
x: x,
y: y,
pageX: i,
pageY: m
});
更新类实例。请注意添加了“customSnap”方法和 flick 事件。
myScroll = new IScroll('#wrapper', {snap: "p"});
myScroll.customSnap = function(data) {
console.log(data);
};
myScroll.on('flick', function() {
console.log(data.currentPage);
});
应该可以了。不一定是最干净的更新,但在我的测试中,它确实有效。