Disable scrolling when touch moving certain element Disable scrolling when touch moving certain element javascript javascript

Disable scrolling when touch moving certain element


Set the touch-action CSS property to none, which works even with passive event listeners:

touch-action: none;

Applying this property to an element will not trigger the default (scroll) behavior when the event is originating from that element.


Note: As pointed out in the comments by @nevf, this solution may no longer work (at least in Chrome) due to performance changes. The recommendation is to use touch-action which is also suggested by @JohnWeisz's answer.

Similar to the answer given by @Llepwryd, I used a combination of ontouchstart and ontouchmove to prevent scrolling when it is on a certain element.

Taken as-is from a project of mine:

window.blockMenuHeaderScroll = false;$(window).on('touchstart', function(e){    if ($(e.target).closest('#mobileMenuHeader').length == 1)    {        blockMenuHeaderScroll = true;    }});$(window).on('touchend', function(){    blockMenuHeaderScroll = false;});$(window).on('touchmove', function(e){    if (blockMenuHeaderScroll)    {        e.preventDefault();    }});

Essentially, what I am doing is listening on the touch start to see whether it begins on an element that is a child of another using jQuery .closest and allowing that to turn on/off the touch movement doing scrolling. The e.target refers to the element that the touch start begins with.

You want to prevent the default on the touch move event however you also need to clear your flag for this at the end of the touch event otherwise no touch scroll events will work.

This can be accomplished without jQuery however for my usage, I already had jQuery and didn't need to code something up to find whether the element has a particular parent.

Tested in Chrome on Android and an iPod Touch as of 2013-06-18


There is a little "hack" on CSS that also allows you to disable scrolling:

.lock-screen {    height: 100%;    overflow: hidden;    width: 100%;    position: fixed;}

Adding that class to the body will prevent scrolling.