javascript 如何使用 e.preventDefault(); 在 ipad/iphone 中禁用、启用,然后再次禁用滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3548014/
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
How to disable, enable, then disable again scrolling in ipad/iphone with e.preventDefault();?
提问by cat
I have it disabled already, and I can enable it again. I used:
我已经禁用它,我可以再次启用它。我用了:
document.ontouchmove = function(e){
e.preventDefault();
}
In the document.ready(); to disable it.
在文档中。ready(); 禁用它。
And I used this to enable it.
我用它来启用它。
function doTouchMove(state) {
document.ontouchmove = function(e){
return state;
}
}
I have it so when a user double clicks on an element, the doTouchMove is called. But how do I make it disabled again?
我有它,所以当用户双击一个元素时,会调用 doTouchMove。但是我如何使它再次禁用?
Thanks
谢谢
回答by Andy E
You could create a toggle instead, where your doTouchMove()function switches between falseand trueevery time it's called:
您可以创建一个切换,每次调用时,您的doTouchMove()函数都会在false和true之间切换:
(function () { // Set up a closure so we don't pollute the global scope
var state = false;
function doTouchMove() {
state = !state;
}
document.ontouchmove = function(e){
return state;
}
document.getElementById("myDoubleClickElement").ondblclick = doTouchMove;
})();
Now, every time you double-click #myDoubleClickElement, it will toggle the statevariable's value between falseand true, effectively disabling on the even clicks and enabling on the odd clicks.
现在,每次双击#myDoubleClickElement 时,它都会在false和true之间切换状态变量的值,从而有效地禁用偶数点击并启用奇数点击。
回答by cat
Im the same user who asked this question... but I cleared my history & everything so I can't pick an answer or anything!
我是问这个问题的同一用户......但我清除了我的历史记录和所有内容,所以我无法选择答案或任何东西!
But what I did to fix it was put this
但我所做的修复它是把这个
document.ontouchmove = function(e){
e.preventDefault();
}
Into its own function, just as the doTouchMove() was. Then when I wanted it to stop moving again i would just call the name of that preventDefault function.
进入它自己的函数,就像 doTouchMove() 一样。然后当我希望它再次停止移动时,我会调用该 preventDefault 函数的名称。
I don't know why it makes a difference but it works! :)
我不知道为什么它会有所不同,但它有效!:)

