移动端网页嵌套一个div,需要滚动条,但是在移动端浏览器上面没办法滚的问题

这里不是点名,但是假如各位在安卓机子(即使是安卓4)下面用过qq浏览器,那么标题这个bug就很明显了。

下面就是各种解决方案。


Android browser bug? div overflow scrolling

Can you make the overflow content of a div scrollable in the Android browser?

It is scrollable in all other modern browsers.

In iOS it is scrollable - however it does not show scrollbars - but it is scrollable via dragging.

A simple example: http://jsfiddle.net/KPuW5/1/embedded/result/

Will this be fixed soon?

share improve this question
 
2 
It is a bug: code.google.com/p/android/issues/detail?id=6864 There are several work-arounds:cubiq.org/iscroll-4 Rather than these hacks - the bug should be fixed, with all the hype around HTML I am surprised how poorly it is implemented. –   mercador  Nov 4 '11 at 18:49 

5 Answers

Android 3.0 and higher have support for overflow:scroll, on < 3.0 it's another story. You might have some success with polyfills like iScroll, however that does come at a cost. It's difficult to implement on sites with complex layouts, and you need to a call a method everytime the content on your site changes. Memory use is also an issue: on already underpowered devices performance may lag because of these kinds of polyfills.

I would recommend a different approach: use Modernizr to detect support for overflow scrolling , add a class to your html tag and use that to rewrite your CSS so that pages scroll 'normally' instead of in a box.

/* For browsers that support overflow scrolling */
#div {
    height: 400px;
    overflow: auto;
}

/* And for browsers that don't */
html.no-overflowscrolling #div {
    height: auto;
}
share improve this answer
 
5 
I think your recommended approach is based on a misconception. Modernizr's overflowscrolling test is not for whether overflow:scroll works in the browser, but whether the browser has support for the *-overflow-scrolling css property, which is different. –   speedarius  Nov 15 '13 at 22:13

overflow: scroll; is supported as of Android 3 (API 11).

For a cross-platform (namely iOS <=4.3.2) Cubiq iScroll is an easy-to-implement fix.

share improve this answer
 

You could try touchscoll.js for scrollable div elements

share improve this answer
 

Just for completeness:

The scrollbars are actually there in Android 2.3, but they are very buggy and by default they are styled to have 0 width, so they are invisible.

You can make them visible by adding styling like:

::-webkit-scrollbar {
    width: 30px;
}
::-webkit-scrollbar-track {
    background-color: $lightestgrey;
}
::-webkit-scrollbar-thumb {
    background-color: $lightgrey;
}

However, the thumb element is not draggable, you can only move it by tapping the track underneath or above it.

Also, these styles will change the look of your scrollbars in all webkit browsers, so best you should add a class that only applies to Android 2.3.

share improve this answer
 

You can make a DIV scrollable in Android by defining the styles of the scrollbar in your CSS. This article show you how to do it: http://css-tricks.com/custom-scrollbars-in-webkit/

share improve this answer
 
 
Could you include a short sample here, to prevent this answer from becoming useless if/when that link goes dead? –   Andrew Barber  Oct 2 '12 at 13:48
 
This does indeed show scrollbars on Android 2.3, but doesn't make the div scrollable :( –   Husky  Oct 2 '12 at 13:57
 
Husky, I try it on an Android 2.3 and it works. Maybe behavior varies in different devices. Try with this webapp: applications.frenys.com/webapp/?ref=dl&app=116815031665454 (you must touch/click on the empty part of the scrollbar track) –   juamp  Oct 19 '12 at 14:57 
 
@juamp How does this answer the question? –   hitautodestruct  Jan 20 '13 at 21:00
 
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficientreputation you will be able to comment on any post. –   chintan khetiya  Sep 30 '13 at 12:31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值