4

A question about GWT ScrollPanel.

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

(Of course, ScrollPanel contains DecoratorPanel that has HTML object)

ljh131
  • 113
  • 10

3 Answers3

1

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;
}
Massi
  • 87
  • 3
0

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);
}
Jake W
  • 2,559
  • 31
  • 36
0

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

Community
  • 1
  • 1
Peter Knego
  • 78,855
  • 10
  • 118
  • 147