2014-08-27 68 views
1

我用下面的iScroll 5代码(通常不那么重要:只是一个普通的滚动页逐页):iScroll页当前滚动到检测

 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(); 
     }); 

那么,有没有任何机会检测beforeScrollStartscrollStart该页面我要滚动到?知道触发该页面项目动画很重要。谢谢!

回答

1

我已经使用了iScroll多年(这是一个优秀的库),我不知道这样做的内置方法。确定iScroll捕捉之前的所有滚动事件(滚动结束除外)。但是,对图书馆稍作修改,我相信这是可能的。

首先,进入iScroll.js源代码并找到_nearestSnap方法。在该方法的底部,你会发现你寻找的对象返回。在返回之前,获取数据并将其传递给自定义事件。不幸的是,iScroll的事件系统不允许你将自定义变量传递给事件,所以你必须做一个解决方法。另外,您需要跟踪“轻弹”事件,因为它不会触发_nearestSnap方法。

iScroll修饰_nearestSnap方法

this.customSnap({ 
    x: x, 
    y: y, 
    pageX: i, 
    pageY: m 
}); 

更新到类实例。请注意添加“customSnap”方法和轻弹事件。

myScroll = new IScroll('#wrapper', {snap: "p"}); 
myScroll.customSnap = function(data) { 
    console.log(data); 
}; 

myScroll.on('flick', function() { 
    console.log(data.currentPage); 
}); 

这应该做到这一点。不一定是最干净的更新,但在我的测试中,它确实有效。

http://jsfiddle.net/9pa4th4y/