Scrolling to the top of a page in a gwt application
Asked Answered
D

1

8

I've got a gwt application and I want to scroll to the top of a page using this method:

public static native void scrollTop() /*-{
  $wnd.scroll(0, 0);
}-*/;

The method is called in the onClick-method of a TreeNodeListenerAdapter:

new TreeNodeListenerAdapter() {
  @Override
  public void onClick(Node node, EventObject e) {
    scrollTop();
  }
}

This does not work and I don't know why. When I put an alert inside my method:

$wnd.alert("Treenode clicked");

I get to see the alert but the page is not scrolled. What am I missing here?

Diligence answered 7/7, 2010 at 12:51 Comment(4)
I don't know why your code doesn't work, but you might consider using the GWT static method Window.scrollTo instead of your native method (see google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/…).Mite
Thanks for the info, but that doesn't work either.Diligence
Weird stuff. I just tried Window.scrollTo and it worked fine in IE and FF4b1. What browser are you having issues with?Loudmouth
Firefox 3.6 and ie6. A simple testpage with the Javascript and without all the gwt and ext-gwt stuff works fine in both browsers, I tried that. So I guess there is something else active that blocks my attempts to scroll the page :/Diligence
A
18

If you want to scroll to the top of a page just do:

Window.scrollTo (0 ,0);

Just be sure that you are importing the correct package com.google.gwt.user.client.Window

Anticipant answered 18/2, 2011 at 12:30 Comment(1)
I actually can't remember anymore what the solution to my problem was, but I solved it somehow. Since this should normally work, I'll accept it as answer.Diligence

© 2022 - 2024 — McMap. All rights reserved.