如何在 GWT 中滚动到 div id
我有一个带有 iframe 的网页,其中正在运行 gwt 应用程序。该网页有一个
。无法在 iframe 中滚动(预期),因此网页的高度为 1000px。最底部有一个按钮,我希望当有人单击该按钮时(注意:该按钮位于 gwt 应用程序中),我想滚动到顶部。这意味着 iframe 需要强制父窗口滚动到顶部。我用这样的 jsni 函数尝试过:
public static native void scrollToTop() /*-{
$wnd.top.scrollTo(0,0);
}-*/;
但这不起作用。所以我的新想法是滚动到 div id“header”。有谁知道如何做到这一点?
我这样尝试:
document.getElementById('header').scrollIntoView();
但这似乎不起作用(因为它应该以 JSNI 方式?)。
感谢您的任何意见!
I have a webpage with an iframe in which a gwt application is running. The webpage has a <div id="head">
. It is not possible to scroll in the iframe (intended) so the webpage has for example a height of 1000px. At the very bottom there is a button and I want that when someone clicks on that button (note: the button is in the gwt application) then I want to scroll to the top.
That means that the iframe needs to force the parent window to scroll to the top. I tried it with a jsni function like this:
public static native void scrollToTop() /*-{
$wnd.top.scrollTo(0,0);
}-*/;
But this didn't work. So my new idea is to scroll to the div id "header". Does anyone know how to accomplish this?
I tried it like this:
document.getElementById('header').scrollIntoView();
But that seems not to work (because it should be in a JSNI manner?).
Thanks for any inputs!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它会失败,因为 GWT 在 iframe 中运行,因此
document
引用 GWT 的 iframe,而不是您的“HTML 主机页”。您必须在 JSNI 中使用$doc
来引用文档(就像$wnd
而不是window
)。但实际上你并不需要 JSNI;普通的旧 Java/GWT 就可以了:
It fails because GWT runs in an iframe, so
document
references the GWT's iframe, and not your "HTML host page". You have to use$doc
in JSNI to reference the document (just like$wnd
instead ofwindow
).But you actually don't need JSNI for that; plain old Java/GWT will do:
最后我找到了解决方案。您必须在网站顶部(iframe 内的网站)设置一个锚点。请查看我的其他 帖子了解更多详情。
PS 我希望您看到这篇文章的重要性并投票。这将为其他人节省很多时间。
Finally I found the solution. You have to set an anchor at the top of your website (the website inside the iframe). Please look at my other post for more details.
P.S. I hope you see the importance of this post and upvote it. It will save a lot of time for other people.