Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent scrolling when touching a certain html element on a touch device?

I have a simple browser game which is finished on desktop, but which I am trying to make work on mobile.

The current problem is that when the user uses a touchscreen to make a vertical dragging motion within the game’s container, it either scrolls the screen or tries to refresh the page depending on direction and level of scroll.

I have tried setting onScroll, onTouchMove, onTouchStart, onDragStart, and onDrag events, all with both e.stopPropagation() and e.preventDefault() as well as returning false but the problem persists.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know it is possible because if you open 2048‘s browser implementation on your phone and try dragging within the game container, it does not scroll, yet you can scroll if you drag anywhere on the page outside the game container. I couldn’t find how it’s done in the page’s source, though.

How can this be accomplished?

>Solution :

Using the following:

* {
...
user-select: none;
touch-action: none;
...
}

shall resolve the problem.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading