最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing hammer events - Stack Overflow

programmeradmin8浏览0评论

I create an event using hammer.js library like this:

Hammer(myElement).on("doubletap", function(evt){
            evt.preventDefault();
        });

How can I then remove the registred event? Can I also use Jquery?

I create an event using hammer.js library like this:

Hammer(myElement).on("doubletap", function(evt){
            evt.preventDefault();
        });

How can I then remove the registred event? Can I also use Jquery?

Share Improve this question asked Mar 15, 2013 at 8:26 JacobJacob 4,03123 gold badges91 silver badges151 bronze badges 1
  • 2 I have no experience of Hammer, but since the syntax looks very similar to jQuery, maybe Hammer uses .off() just like jQuery does? That is just a wild guess though. – Christofer Eliasson Commented Mar 15, 2013 at 8:29
Add a ment  | 

2 Answers 2

Reset to default 16

It's simply Hammer(myElement).off(eventName);

If you want to use jQuery, then the syntax is:

$(myElement).hammer().on(eventName, callback)

If you want to specify "namespace" for the event, then you declare eg.

$(myElement).hammer().on("tap.namespace", callback);
$(myElement).hammer().on("tap.anotherNamespace", callback2);

which makes it possible to detach only desired event, eg:

$(myElement).hammer().off("tap.anotherNamespace");

In the documentation of hammer.js API, it is stated that it will not remove the dom events, using $(element).off() will not resolve your issues, a work around is to use the hammer.js jquery wrapper, their events are registered on an element using the bind function. Use the unbind to remove all or specific events.

$(element).hammer().unbind();

$(element).hammer().bind('swipeleft', 'swiperight', function(){});

The wrapper is here: https://github./hammerjs/jquery.hammer.js/blob/master/jquery.hammer.js

发布评论

评论列表(0)

  1. 暂无评论