简体   繁体   中英

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(); 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. 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:

(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.

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.

I don't know why it makes a difference but it works! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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