开发者

How to know which element is being displayed in GWT ScrollPanel

A question about GWT ScrollPanel.

Is there any way to determine which child element is being displayed in ScrollPanel?

(Of cours开发者_如何转开发e, ScrollPanel contains DecoratorPanel that has HTML object)


Here's the GWT method that does the Job (it is translated from the JQuery solution suggested above).

    /**
 * @param widget the widget to check
 * @return true if the widget is in the visible part of the page
 */
private boolean isScrolledIntoView(Widget widget) {
    if (widget != null) {
        int docViewTop = Window.getScrollTop();
        int docViewBottom = docViewTop + Window.getClientHeight();
        int elemTop = widget.getAbsoluteTop();
        int elemBottom = elemTop + widget.getOffsetHeight();
        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }       
    return false;
}


AFAIK there is no built-in functionality to do so, not in DOM nor in GWT.

If you have fixed height elements you might try to calculate if certain element is shown: Check if element is visible after scrolling


If you want to check if a widget is visible in a scroll panel, you can do it in this way (Similar to Massi's solution):

  /**
   * @param widget the widget to check
   * @return true if the widget is in the visible part of the scroll panel
   */
  private boolean isVisibleInScrollPanel(Widget widget, ScrollPanel scrollPanel) {
    if (widget != null) {
      int containerTop = scrollPanel.getAbsoluteTop();
      int containerBottom = containerTop + scrollPanel.getOffsetHeight();
      int widgetTop = widget.getAbsoluteTop();
      int widgetBottom = widgetTop + widget.getOffsetHeight();
      return ((widgetBottom <= containerBottom) && (widgetTop >= containerTop));
    }
    return false;
  }

I used this method in this way:

// When the selected widget is invisible, then we ensure it to be visible
if (!isVisibleInScrollPanel(widget, scrollPanel)) {
  scrollPanel.ensureVisible(widget);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜