[°³¹ß Tip] webview ½ºÅ©·Ñ |
|
|
 |
8³â Àü |
Ãâó: http://stackoverflow.com/questions/2238938/how-to-programmatically-scroll-android-webview
It turns out that the window.scrollTo() DOES work, you just can't add your own method named scrollTo(). For some reason my own scrollTo() method was being invoked when I called window.scrollTo().
So, in summary, to scroll a WebView to a particular DOM element, write a JavaScript function to do the scrolling:
function scrollToElement(id) {
var elem = document.getElementById(id);
var x = 0;
var y = 0;
while (elem != null) {
x += elem.offsetLeft;
y += elem.offsetTop;
elem = elem.offsetParent;
}
window.scrollTo(x, y);
}
and then from your Android app (Java code), tell your WebView to load a URL:
webView.loadUrl("javascript:scrollToElement('" + elemId + "')");
There are some issues with this approach, such as the scroll will not be nicely animated, but the general mechanism works.
The DOM window object does report the current scroll position of the WebView correctly (see window.pageXOffset, window.pageYOffset or window.scrollX, window.scrollY). If you just want to know the current scroll position of the WebView, write some JavaScript to call your Java code and pass along the X/Y offsets.
Ȥ½Ã À¥ºä ¾²½Ã´Ù°¡ ¿·¿¡ ½ºÅ©·Ñ Èò»ö °ø°£ÀÌ °Å½½¸®½Ã´Â ºÐµéÀº
webview.setVerticalScrollbarOverlay(true);
Ãß°¡ÇØÁÖ½Ã¸é µÇ°Ù½À´Ï´ç
|
|
̵̧ : 380 |
̵̧
¸ñ·Ï
|
|