Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile overflow:scroll and overflow-scrolling: touch // prevent viewport "bounce"

On a mobile (Safari, webviews, wherever), overflow:scroll and overflow-scrolling: touch give a pretty smooth scroll, which is cool.

But, it makes the page "bounce" (area circled below), which is not the case when you are not using it, but which makes the experience a little less "native" (and more simply, as far as I can have an opinion about it, is absolutely un-useful)

Is there a way to prevent it to happen?

that kind of suxx

like image 909
Ben Avatar asked Mar 31 '13 17:03

Ben


People also ask

What does overscroll behavior do?

The overscroll-behavior CSS property sets what a browser does when reaching the boundary of a scrolling area.

What is scroll bounce?

Scroll bouncing (also sometimes referred to as scroll 'rubber-banding', or 'elastic scrolling') is often used to refer to the effect you see when you scroll to the very top of a page or HTML element, or to the bottom of a page or element, on a device using a touchscreen or a trackpad, and empty space can be seen for a ...

What is the difference between overflow scroll and overflow auto?

The difference For that, you need overflow: auto , which lets a browser automatically determine if a scroll bar is needed — or not. So in short: overflow: scroll : Always show a scroll bar. overflow: auto : Show a scroll bar when needed.

Does overflow hidden prevent scrolling?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML.


1 Answers

There's a great blog post on this here:

http://www.kylejlarson.com/blog/2011/fixed-elements-and-scrolling-divs-in-ios-5/

Along with a demo here:

http://www.kylejlarson.com/files/iosdemo/

In summary, you can use the following on a div containing your main content:

.scrollable {     position: absolute;     top: 50px;     left: 0;     right: 0;     bottom: 0;     overflow: scroll;     -webkit-overflow-scrolling: touch; } 

The problem I think you're describing is when you try to scroll up within a div that is already at the top - it then scrolls up the page instead of up the div and causes a bounce effect at the top of the page. I think your question is asking how to get rid of this?

In order to fix this, the author suggests that you use ScrollFix to auto increase the height of scrollable divs.

It's also worth noting that you can use the following to prevent the user from scrolling up e.g. in a navigation element:

document.addEventListener('touchmove', function(event) {    if(event.target.parentNode.className.indexOf('noBounce') != -1  || event.target.className.indexOf('noBounce') != -1 ) {     event.preventDefault(); } }, false); 

Unfortunately there are still some issues with ScrollFix (e.g. when using form fields), but the issues list on ScrollFix is a good place to look for alternatives. Some alternative approaches are discussed in this issue.

Other alternatives, also mentioned in the blog post, are Scrollability and iScroll

like image 181
Alasdair McLeay Avatar answered Sep 21 '22 10:09

Alasdair McLeay