How to know which element is being displayed in GWT ScrollPanel

混江龙づ霸主 提交于 2019-12-23 12:31:17

问题


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)


回答1:


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




回答2:


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;
}



回答3:


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);
}


来源:https://stackoverflow.com/questions/7225660/how-to-know-which-element-is-being-displayed-in-gwt-scrollpanel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!