简体   繁体   English

如何禁用,启用然后再次禁用使用e.preventDefault();在ipad / iphone中滚动?

[英]How to disable, enable, then disable again scrolling in ipad/iphone with e.preventDefault();?

I have it disabled already, and I can enable it again. 我已经禁用了它,可以再次启用它。 I used: 我用了:

document.ontouchmove = function(e){
             e.preventDefault();
}

In the document.ready(); 在document.ready();中 to disable it. 禁用它。

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. 我有它,所以当用户双击某个元素时,就会调用doTouchMove。 But how do I make it disabled again? 但是如何再次禁用它?

Thanks 谢谢

You could create a toggle instead, where your doTouchMove() function switches between false and true every time it's called: 您可以创建一个切换,每次调用doTouchMove()函数在falsetrue之间切换时:

(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 state variable's value between false and true , effectively disabling on the even clicks and enabling on the odd clicks. 现在,每次双击#myDoubleClickElement ,它将在falsetrue之间切换状态变量的值,从而有效地禁用偶数点击和奇数点击。

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. 就像doTouchMove()一样,将其放入自己的函数中。 Then when I wanted it to stop moving again i would just call the name of that preventDefault function. 然后,当我希望它再次停止移动时,我只需调用该preventDefault函数的名称即可。

I don't know why it makes a difference but it works! 我不知道为什么会有所不同,但行得通! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM