I created a website with jQueryMobile for iOS and Android.
I don't want the document itself to scroll. Instead, just an area (a <div>
element) should be scrollable (via css property overflow-y:scroll
).
So I disabled document scrolling via:
$(document).bind("touchstart", function(e){
e.preventDefault();
});
$(document).bind("touchmove", function(e){
e.preventDefault();
});
But that will also disable scrolling for all other elements in the document, no matter if overflow:scroll
is set or not.
How can I solve this?
Here is my implementation which works on touch devices and laptops.
Maybe I misunderstood the question, but if I'm correct:
You want not to be able to scroll except a certain element so you:
Prevent everything within the document.
Why don't you just stop the event bubbling on the element where you wish to scroll? (PS: you don't have to prevent touchstart -> if you use touch start for selecting elements instead of clicks that is prevented as well, touch move is only needed because then it is actually tracing the movement)
Now on the element CSS
If you are on a mobile device, you can even go a step further. You can force hardware accelerated scrolling (though not all mobile browsers support this);
normal:
native feel:
Here is a solution I am using:
$scrollElement is the scroll element, $scrollMask is a div with style
position: fixed; top: 0; bottom: 0;
. Thez-index
of $scrollMask is smaller than $scrollElement.Here is a solution that uses jQuery for the events.