2014-08-27 60 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/