per_jansson
per_jansson

Reputation: 2189

Determine if a widget is showing in the viewport in GWT

Is it possible to determine wether or not a specific widget is showing in the viewport?

I want to start loading more data once the last widget in my listwidget is displayed to the user. I figured I could handle scroll events and for every event check if the last widget is displayed and the fire of an rpc to get more data.

BR

Upvotes: 2

Views: 1661

Answers (2)

Massi
Massi

Reputation: 107

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

/**
 * @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;
}

Upvotes: 1

Thomas Broyer
Thomas Broyer

Reputation: 64561

Have a look at the ShowMorePagerPanel from the GWT Showcase's CellList sample.

You can use the widget's getElement()'s getOffsetTop() and the scrolling element's getScrollTop() (equivalent to getVerticalScrollPosition() for a ScrollPanel) to compute whether the widget is currently in view (beware of the elements' getOffsetParent() though, you might want to set the scrolling element's CSS position to relative to make it the offset parent of the widget)

Upvotes: 0

Related Questions