0

I have a clickhandler for a button. I want the screen scrolls to an anchor like

Anchor a = new Anchor();
a.setName("stopHere");

(...)

button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
    // Do somethings
            (...)

            // Scroll to Anchor "stopHere"

            ?????
}
});

How can I do it?

This is similar when using an anchor <a href="#stopHere"> click to go to stopHere point </a>, but I wan do it next the button is clicked.

Thanks.

  • Does http://stackoverflow.com/questions/5402732/gwt-set-url-without-submit help? I didn't try it but setting the hash token in the URL should do the trick I think. – z00bs Apr 07 '14 at 11:47

1 Answers1

1

The method Element#scrollIntoView() should do what you want:

final Anchor a = new Anchor();
a.setName("stopHere");

button.addClickHandler(new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        a.getElement().scrollIntoView();
    }
}

Or use Window#scrollTo(int, int)

final Anchor a = new Anchor();
a.setName("stopHere");

button.addClickHandler(new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        Window.scrollTo(a.getAbsoluteLeft(), a.getAbsoluteTop());
    }
}
Baz
  • 36,440
  • 11
  • 68
  • 94