jQuery:使用 touchmove 禁用和启用触摸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14355365/
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
jQuery: disable and enable touch with touchmove
提问by Morteza
I use this code to disable and enable touch:
我使用此代码禁用和启用触摸:
jQuery(document).ready(function(){
jQuery("body").on("touchmove", false);
jQuery('button').click(function(){
jQuery("body").on("touchmove", true);
});
});
function work fine for disabling touch but after click on button touch cannot be enable.
禁用触摸功能工作正常,但单击按钮后无法启用触摸。
whats wrong on code?
代码有什么问题?
回答by Denys Séguret
What is wrong is simply that you don't pass the argument required by on. You should pass a function (the event handler) and not just a boolean.
错误的只是您没有传递on所需的参数。您应该传递一个函数(事件处理程序)而不仅仅是一个布尔值。
The simplest way to unbind then rebind is to have a boolean somewhere and test it in your handler. For example :
解除绑定然后重新绑定的最简单方法是在某处设置一个布尔值并在您的处理程序中对其进行测试。例如 :
myapp = {active: true}; // change this boolean
jQuery("body").on("touchmove", function(e) {
if (myapp.active) { // change myapp.active
// do things
}
});
If you want to unbind definitively, instead of passing false
to on
, use off
. Passing false
is a non documented trick that might break on future versions.
如果您想明确解除绑定,而不是传递false
给on
,请使用off
。传递false
是一个没有记录的技巧,可能会在未来的版本中失效。
The off
documentationalso contains an example of another way to bind/unbind a function on click :
该off
文档还包含在单击时绑定/取消绑定函数的另一种方法的示例:
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("body").on("click", "#theone", aClick)
.find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
$("body").off("click", "#theone", aClick)
.find("#theone").text("Does nothing...");
});
回答by bipen
use bind()
and unbind()
使用bind()
和unbind()
try this
尝试这个
jQuery('body').bind('touchmove', function(e){e.preventDefault()});
jQuery('button').click(function(){
jQuery('body').unbind('touchmove');
});