如何在滚动条出现时捕获事件?

时间:2011-06-14 17:43:03

标签: events gwt

我有一个GWT ScrollPanel,其overflow-y设置为auto。当面板的滚动条出现或消失时是否会触发事件?

1 个答案:

答案 0 :(得分:0)

不幸的是,当向某个元素添加或删除滚动条时,不会触发任何事件。然而,推出自己的并不难:

public class MyScrollPanel extends ScrollPanel {
  boolean horizontal = false;
  boolean vertical = false;

  public native int getScrollHeight() /*-{
    return this.@com.mypackage.MyScrollPanel::getElement()().scrollHeight;
  }-*/;

  public native int getScrollWidth() /*-{
    return this.@com.mypackage.MyScrollPanel::getElement()().scrollWidth;
  }-*/;

  private boolean hasHorizontalScrollbar() {
    return getElement().getClientWidth() < getScrollWidth();
  }

  private boolean hasVerticalScrollbar() {
    return getElement().getClientHeight() < getScrollHeight();
  }

  public void onLoad() {
    new Timer() {
      @Override
      public void run() {
        boolean scrollersChanged = false;

        if (hasHorizontalScrollbar() != horizontal) {
          horizontal = !horizontal;
          scrollersChanged = true;
        }

        if (hasVerticalScrollbar() != vertical) {
          vertical = !vertical;
          scrollersChanged = true;
        }

        if (scrollersChanged) {
          // Fire event or call onResize()
        }
      }
    }.scheduleRepeating(500);
  }
}