开发者

Scrolling a page to the top from an iframe (containing a gwt application)

开发者 https://www.devze.com 2023-04-11 14:40 出处:网络
There is a website which h开发者_如何学Pythonas an iframe including a web application. Let\'s assume that the application has a height of 1000px. At the bottom of the application (1000px) there is but

There is a website which h开发者_如何学Pythonas an iframe including a web application. Let's assume that the application has a height of 1000px. At the bottom of the application (1000px) there is button. If the user presses the button, then the application changes its view and the new length is only 20px (the rage 20px to 1000px is therefore just white (blank page) ).

My goal is now that when someone presses on that button then the browser should scroll its page to the top (meaning that the scrolling happens outside the iframe. Does anyone know a solution how to do that in gwt (without setting an anchor in the page holding the iframe)?

Many thx in advance!


Finally I found the solution. Set at the top of your iframe an anchor and call it with:

public static native void scrollToTop()  /*-{

    $wnd.location.href = '#anchorName';

}-*/;

This will also force the parent window (the window holding the iframe) to scroll to that anchor.


Scrolling with javascript

Most people hate iframes for a number of reasons though, you should avoid using them if at all possible.


How about using JSNI and scrolling in parent or top window? Something like this:

public static native void scrollToTop()  /*-{
    $wnd.parent.scrollTo(0,0);  // or `$wnd.top.scrollTo(0,0);
}-*/;

The combination with top should also work with just "window". The $wnd is just because GWT is actually running in an embedded iframe. But I'm not sure if your code will be allowed to scroll parent document if they come from different servers.


What about calling a java script function scrolling to a div id of the page holding the iframe? Is that possible?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号