Hammer js - off() method doesn't work

流过昼夜 提交于 2019-12-11 07:55:12

问题


I am using Hammer this way, it works (it just triggers next/prev buttons):

var slider = document.getElementsByClassName('slider');

Hammer(slider[0]).on("swiperight", function() {
    prev.click();
});

A according to Stack thread, on event when modal is coming out (hammer is still swiping behind it :/) I am trying to turn it of with:

Hammer(slider[0]).off("swipeleft");
Hammer(slider[0]).off("swiperight");

But it doesn't work. It's still swipeing behind modal. Any idea why?


回答1:


Save Hammer instance to a variable. It works this way. The following example will remove pan event after 5 seconds.

var h = new Hammer(document);
  
h.on('pan', function(){
   console.log('Panned');
});
setTimeout(function(){
    console.log('removed');
    h.off('pan');
}, 5000);
<script src="http://hammerjs.github.io/dist/hammer.min.js"></script>


来源:https://stackoverflow.com/questions/39512670/hammer-js-off-method-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!