最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to scroll an HTML DIV programmatically on Android 3.1? - Stack Overflow

programmeradmin1浏览0评论

I can't make the following work on Android 3.1/Honeyb (only Android version tested), but it works everywhere else - including on other WebKit-based browsers, e.g. on iPhone.

A DIV with fixed dimensions:

<DIV id="TestDiv" style="width:300px;height:200px;overflow:auto;">Lorem ipsum dolor sit amet, etc.</DIV>

A line of JavaScript or jQuery to scroll it down after page has loaded:

document.getElementById("TestDiv").scrollTop = 100;
$("#TestDiv").scrollTop(100);

I also tried wrapping the above in a setTimeout to introduce a small delay from the loading of the page. It is sometimes needed with webkit, but it did not help here.

I have seen programmatic DIV scrolling work on my Android 3.1. device so I know it can be done. The question is how?

EDIT: Please notice, I am looking for a web solution that also works in the stock browser of Android, not a native Android solution.

I can't make the following work on Android 3.1/Honeyb (only Android version tested), but it works everywhere else - including on other WebKit-based browsers, e.g. on iPhone.

A DIV with fixed dimensions:

<DIV id="TestDiv" style="width:300px;height:200px;overflow:auto;">Lorem ipsum dolor sit amet, etc.</DIV>

A line of JavaScript or jQuery to scroll it down after page has loaded:

document.getElementById("TestDiv").scrollTop = 100;
$("#TestDiv").scrollTop(100);

I also tried wrapping the above in a setTimeout to introduce a small delay from the loading of the page. It is sometimes needed with webkit, but it did not help here.

I have seen programmatic DIV scrolling work on my Android 3.1. device so I know it can be done. The question is how?

EDIT: Please notice, I am looking for a web solution that also works in the stock browser of Android, not a native Android solution.

Share Improve this question edited Jan 24, 2012 at 8:20 W3Coder asked Jan 20, 2012 at 9:50 W3CoderW3Coder 6208 silver badges21 bronze badges 2
  • are you just trying to move the DIV to the top of the viewport window? Or are you trying to mimic page scrolling? – tkone Commented Jan 31, 2012 at 1:40
  • I am trying to make the text scroll inside the DIV - it is for a chat application. – W3Coder Commented Jan 31, 2012 at 7:24
Add a ment  | 

5 Answers 5

Reset to default 2

This is a known problem with the stock Android browser:

http://code.google./p/android/issues/detail?id=19625

The best solution is probably to wait until it is fixed by Google, rather than trying to figure out a hack.

You can done with normal javascript method. Please kindly check with this

window.scroll(0,650)

0 - X Axis 650 - Y Axis

A workaound that worked for me: first, temporarily set the overflow property to 'hidden', then set the scrollTop property, then set the overflow property back to 'scroll' (or auto). The scrollTop value seems to be kept intact and honored when the overflow property is set back to 'scroll'. This was a pretty trivial workaround that worked on all browsers I tested on (desktop and mobile). I didn't test it exhaustively, and I didn't test with transitions in place, so there may be side-effects that I haven't encountered... Your mileage may vary - but it's an easy thing to try. see: jQuery scrollTop() does not work in scrolling DIV on mobile browsers, alternatives?

what I think of is that you can override onPageFinished(WebView view, String url) and inside it you can do this view.scrollTo(x, y);

I had a similar issue. In the end I just placed each of my pages on top of each other using position absolute with a zindex then toggled their visibility. Sucks that bugs like this exist.

发布评论

评论列表(0)

  1. 暂无评论