2017-06-22 136 views
1

我有一個綁定到mousewheel的事件,我在其中使用該事件進行一些計算,我希望能夠使用相同的函數來執行觸摸設備的相同計算。用觸摸設備觸發mousewheel事件

我的代碼是這樣的:

$('#element').bind("mousewheel DOMMouseScroll", function(event) { 
      event.preventDefault(); 
      if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) 
       //do stuff 
      else { 
       //do other stuff 
      } 
     }); 

編輯:由於從評論中我明白,我是不完全清楚,我的目標是獲得的值等「wheelDelta」,使我認識到,如果用戶正在滾動(touchmoving)向上或向下

+0

沒有'觸摸設備的mousewheel'事件。也許你應該一起使用'mousemove'和'touchmove'? –

+0

你可以試試我的插件[jquery-do-scroll](https://github.com/WashingtonGuedes/jquery-do-scroll)。它適用於鼠標,觸摸和輪子事件。 –

回答

0

這可能不是合適的解決方案,但您可以這樣做。

var lastScroll = 0; 

    $('#element').on("scroll", function() { 
    var st = $(this).scrollTop(); 
    if (st > lastScroll){ 
     // downscroll code 
     //$(this).trigger("mousewheeldown"); 
    } else { 
     // upscroll code 
     //$(this).trigger("mousewheelup"); 
    } 
    lastScroll = st; 
    $(this).trigger("mousewheel"); 
    }); 

我希望幫助:)

+0

他們爲什麼要這樣做? –

+0

他基本上可以檢測到一個用戶生成的事件並手動觸發另一個。根據我的理解,這基本上就是他想要的。 –

+0

有沒有點擊我的代碼我不明白爲什麼我會嘗試像你建議 – Plastic