[英]How can I scroll content of <webview> tag from JavaScript?
I am using <webview>
tag to embed a page. 我正在使用<webview>
标签嵌入页面。 <webview>
tag has shadow-root
which has one tag, <object id="browser-plugin-1 ...>
. So I tried to set scrollTop
value for this tag like this. <webview>
标签有shadow-root
,它有一个标签, <object id="browser-plugin-1 ...>
。所以我试着像这样为这个标签设置scrollTop
值。
var webView = document.getElementById('webview tag id');
var elm = webView.shadowRoot.firstChild; // elm is object tag
console.log(elm.scrollTop); // 0
elm.scrollTop = 100;
console.log(elm.scrollTop); // 0
But nothing happend... 但没有发生任何事......
Is it possible to control <webview>
tag scroll position from outside? 是否可以从外部控制<webview>
标签滚动位置?
Yes, do this instead: 是的,请改为:
var webView = document.getElementById('webview tag id');
webView.executeJavaScript("document.querySelector('body:first-child').scrollTop=100");
It's possible to execute any kind of javascript via the WebView.executeJavaScript(code)
function which will evaluate the code inside the WebView. 可以通过WebView.executeJavaScript(code)
函数执行任何类型的javascript,该函数将评估WebView中的代码。
To access your element you would first have to wait for the WebView to load, and then execute the javascript. 要访问元素,首先必须等待WebView加载,然后执行javascript。
var webView = document.getElementById('webView');
webView.addEventListener('did-finish-load', scrollElement );
function scrollElement(){
var code = "var elm = document.querySelector('body:first-child'); elm.scrollTop = 100;";
webView.executeJavaScript(code);
}
Note : Haven't tested this code, it may have syntax errors. 注意 :未测试此代码,可能会出现语法错误。
Source (AtomShell's WebView tag documentation) Source(AtomShell的WebView标签文档)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.