javascript - How to scroll to a div id in GWT - Stack Overflow

I have a webpage with an iframe in which a gwt application is running. The webpage has a <div id=&qu

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 acplish 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!

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 acplish 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!

Share Improve this question edited Oct 12, 2011 at 15:33 Hilbrand Bouwkamp 13.5k1 gold badge47 silver badges52 bronze badges asked Oct 12, 2011 at 12:46 mknmkn 13.2k17 gold badges52 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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 of window).

But you actually don't need JSNI for that; plain old Java/GWT will do:

Document.get().getElementById("header").scrollIntoView();

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745652643a4638355.html

相关推荐

  • javascript - How to scroll to a div id in GWT - Stack Overflow

    I have a webpage with an iframe in which a gwt application is running. The webpage has a <div id=&qu

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信