Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.ontouchmove and scrolling on iOS 5

iOS 5 has brought a number of nice things to JavaScript/Web Apps. One of them is improved scrolling. If you add

-webkit-overflow-scroll:touch; 

to the style of a textarea element, scrolling will work nicely with one finger.

But there's a problem. To prevent the entire screen from scrolling, it is recommended that web apps add this line of code:

document.ontouchmove = function(e) {e.preventDefault()}; 

This, however, disables the new scrolling.

Does anyone have a nice way to allow the new scrolling within a textarea, but not allow the whole form to scroll?

like image 787
ghenne Avatar asked Oct 17 '11 18:10

ghenne


1 Answers

Update Per Alvaro's comment, this solution may no longer work as of iOS 11.3.

You should be able to allow scrolling by selecting whether or not preventDefault is called. E.g.,

document.ontouchmove = function(e) {     var target = e.currentTarget;     while(target) {         if(checkIfElementShouldScroll(target))             return;         target = target.parentNode;     }      e.preventDefault(); }; 

Alternatively, this may work by preventing the event from reaching the document level.

elementYouWantToScroll.ontouchmove = function(e) {     e.stopPropagation(); }; 

Edit For anyone reading later, the alternate answer does work and is way easier.

like image 102
Brian Nickel Avatar answered Sep 16 '22 23:09

Brian Nickel