window.scrollY in GWT

100 views
Skip to first unread message

Jon Brisbin

unread,
Aug 4, 2008, 10:56:42 PM8/4/08
to Google Web Toolkit
Window.getScrollTop() doesn't return the same value as window.scrollY
after a scroll event. On the iPod touch (2.0 firmware), in my
ontouchend handler, Window.getScrollTop() keeps returning the same
value after multiple scrolls. If I use a native method to return
"window.scrollY" I get the constantly updated value of how far down
the page the user has scrolled (which is what I need).

Why wouldn't the GWT method return the expected value? Or am I
thinking it should do something it wasn't intended to?

Thanks!

Jon Brisibn
http://jbrisbin.com

Fred Sauer

unread,
Aug 4, 2008, 11:28:28 PM8/4/08
to Google-We...@googlegroups.com
Jon,

You might want to check this thread:
  http://groups.google.com/group/Google-Web-Toolkit-Contributors/browse_thread/thread/f31e5e469d200de

You can follow up on that thread and in the issue tracker.

(Note: the changes from the above thread were committed in r3364 on the 1.5 release branch.)

Fred Sauer
fr...@allen-sauer.com

Jon Brisbin

unread,
Aug 5, 2008, 12:00:58 AM8/5/08
to Google-We...@googlegroups.com
Since this just went in a couple days ago, I'll wait for it to appear in trunk (which is what I'm keeping up-to-date with) and rework the native hacks then...

thanks for the info!

In case anyone's interested, I implemented a poor-man's onTouchEvent mechanism by putting another utility method on my BrowserUtils class that attaches a standard EventListener interface to the ontouchstart,end,move events for an element:

  public static native void addTouchListener( Element el, EventListener listener )
  /*-{
    el.ontouchstart = listener.@com.google.gwt.user.client.EventListener::onBrowserEvent(Lcom/google/gwt/user/client/Event;);
    el.ontouchmove = listener.@com.google.gwt.user.client.EventListener::onBrowserEvent(Lcom/google/gwt/user/client/Event;);
    el.ontouchend = listener.@com.google.gwt.user.client.EventListener::onBrowserEvent(Lcom/google/gwt/user/client/Event;);
  }-*/;

I'm not much up on the intricacies of memory leaks and event handlers, so I can't say that this safe for long-running applications. But it works for me with the big caveat that I can't access my instance objects from the methods called on the event. For example:

  public void onBrowserEvent( Event event )
  {
    String type = event.getType();
    if ( type.equals( "touchstart" ) ) {
      DOM.getElementById( "header" ).getStyle().setProperty( "opacity", "0" );
      DOM.getElementById( "footer" ).getStyle().setProperty( "opacity", "0" );
    } else if ( type.equals( "touchend" ) ) {
      final Style headerStyle = DOM.getElementById( "header" ).getStyle();
      final Style footerStyle = DOM.getElementById( "footer" ).getStyle();

      new Animation()
      {

        protected void onUpdate( double progress )
        {
          int scrollY = BrowserUtils.getScrollY();
          headerStyle.setPropertyPx( "top", scrollY );
          headerStyle.setProperty( "opacity", "" + ( 1 * progress ) );
          footerStyle.setPropertyPx( "top", ( ( BrowserUtils.getInnerHeight() - 36 ) + scrollY ) );
          footerStyle.setProperty( "opacity", "" + ( 1 * progress ) );
        }

      }.run( 125, Duration.currentTimeMillis() + 300 );
    }
  }

I have to go through some machinations to get references to the objects that are defined as instance variables. No matter what I did, when the event was triggered (maybe because the event firer is a native trigger and not something else...don't know) my instance variables were not in scope, and thus undefined. That's probably a bug and I'll post an issue on it. Rather than being annoying, it could contribute to some really hard-to-debug errors because you wouldn't think that referencing your instance methods would one way in a java method, but after being called from JavaScript in connection with an event, they are non-referenecable.

Thanks!

Jon Brisibn
Reply all
Reply to author
Forward
0 new messages