javascript 删除锤子事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15427715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:44:41  来源:igfitidea点击:

Removing hammer events

javascripthammer.js

提问by Jacob

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

我使用hammer.js 库创建一个事件,如下所示:

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

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

我怎样才能删除注册的事件?我也可以使用 Jquery 吗?

回答by Samuli Hakoniemi

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

简直就是 Hammer(myElement).off(eventName);

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

如果你想使用jQuery,那么语法是:

$(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");

回答by Pills

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.

在hammer.js API 的文档中,声明它不会删除dom 事件,使用$(element).off() 不会解决您的问题,解决方法是使用hammer.js jquery 包装器,它们的使用 bind 函数在元素上注册事件。使用 unbind 删除所有或特定事件。

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

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

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

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

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

包装器在这里:https: //github.com/hammerjs/jquery.hammer.js/blob/master/jquery.hammer.js