Prevent page scroll on drag in IOS and Android

后端 未结 5 797
既然无缘
既然无缘 2020-12-30 00:49

I\'m working on a html5 canvas game, but I don\'t know how to handle touch events. When a user touch the screen, and drag, then the browser will scroll the page. I would lik

相关标签:
5条回答
  • 2020-12-30 01:12
    canvas.addEventListener('touchstart', function(e)
    {
        alert(e.changedTouches[0].pageX + " " + e.changedTouches[0].pageY);
    }
    canvas.addEventListener('touchend', function(e)
    {
        alert(e.changedTouches[0].pageX + " " + e.changedTouches[0].pageY);
    }
    

    Here's a good article about touching and gesturing on mobile phones:

    http://www.sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/

    0 讨论(0)
  • 2020-12-30 01:13

    You need to override the default touch behaviour to stop touchevents dragging the page. Clearly, you'll need to handle them again if your page becomes larger than the available area, but as you're making a game, going to assume you're doing 100%/100% layout.

    function preventBehavior(e) {
        e.preventDefault(); 
    };
    
    document.addEventListener("touchmove", preventBehavior, {passive: false});
    

    Edit: here's the W3C recommendation talking about touch events, which might be handy for you.

    0 讨论(0)
  • 2020-12-30 01:13

    Due to breaking changes made in recent versions of Chrome, the above answers are no longer correct. Attaching a touch event listener to the document or body elements will cause the event listener to be registered in "passive" mode, which causes calls to preventDefault to be ignored.

    There are two solutions:

    • The preferred solution is to use the CSS style touch-action to specify that no scrolling should happen (e.g. with the value "none")

    • In cases where this is not appropriate (e.g. if the type of interaction should change dynamically in a way that cannot be determined before the gesture begins) then the event listener must be registered with the third parameter set to { passive: false } (you should perform browser detection to ensure that this style is supported first, though).

    0 讨论(0)
  • 2020-12-30 01:23

    Following solution preventing scroll when dragging AND at the same time usual scroll is working (when not dragging)

    var scrollable = true;
    
    var listener = function(e) {
        if (! scrollable) {
            e.preventDefault();
        }
    }
    
    document.addEventListener('touchmove', listener, { passive:false });
    
    
    onDragstartHandler() {
      scrollable = false;
    }
    
    onDragendHandler(} {
      scrollable = true;
    }
    

    Don't forget to bind onDragstartHandler and onDragendHandler to related elements

    0 讨论(0)
  • 2020-12-30 01:25

    If you don't want to use jQuery mobile or any other library then you can try this.

    var startX, startY, endX, endY;
    document.addEventListener("touchstart", function(e){
        startX = e.touches[0].pageX;
        startY = e.touches[0].pageY;
    
        e.preventDefault();//Stops the default behavior
    }, false);
    
    document.addEventListener("touchend", function(e){
        endX = e.touches[0].pageX;
        endY = e.touches[0].pageY;
    
        e.preventDefault();//Stops the default behavior
    }, false);
    
    0 讨论(0)
提交回复
热议问题