B2G/gecko/browser/devtools/shared/LayoutHelpers.jsm
/**
   * Scroll the document so that the element "elem" appears in the viewport.
   *
   * @param Element elem the element that needs to appear in the viewport.
   * @param bool centered true if you want it centered, false if you want it to
   * appear on the top of the viewport. It is true by default, and that is
   * usually what you want.
   */
Scroll the document so that the element "elem" appears in the viewport.
@param Element elem the element that needs to appear in the viewport.
@param bool centered true if you want it centered, false if you want it to
appear on the top of the viewport. It is true by default, and that is
usually what you want.
Scroll the document so that the element " elem " appears in the viewport . @param Element elem the element that needs to appear in the viewport . @param bool centered true if you want it centered false if you want it to appear on the top of the viewport . It is true by default and that is usually what you want .
function LH_scrollIntoViewIfNeeded(elem, centered) {
    centered = centered === undefined? true: !!centered;
    let win = elem.ownerDocument.defaultView;
    let clientRect = elem.getBoundingClientRect();
    let topToBottom = clientRect.bottom;
    let bottomToTop = clientRect.top - win.innerHeight;
    let leftToRight = clientRect.right;
    let rightToLeft = clientRect.left - win.innerWidth;
    let xAllowed = true;  
    let yAllowed = true;  
    if ((topToBottom > 0 || !centered) && topToBottom <= elem.offsetHeight) {
      win.scrollBy(0, topToBottom - elem.offsetHeight);
      yAllowed = false;
    } else
    if ((bottomToTop < 0 || !centered) && bottomToTop >= -elem.offsetHeight) {
      win.scrollBy(0, bottomToTop + elem.offsetHeight);
      yAllowed = false;
    }
    if ((leftToRight > 0 || !centered) && leftToRight <= elem.offsetWidth) {
      if (xAllowed) {
        win.scrollBy(leftToRight - elem.offsetWidth, 0);
        xAllowed = false;
      }
    } else
    if ((rightToLeft < 0 || !centered) && rightToLeft >= -elem.offsetWidth) {
      if (xAllowed) {
        win.scrollBy(rightToLeft + elem.offsetWidth, 0);
        xAllowed = false;
      }
    }
    if (centered) {
      if (yAllowed && (topToBottom <= 0 || bottomToTop >= 0)) {
        win.scroll(win.scrollX,
                   win.scrollY + clientRect.top
                   - (win.innerHeight - elem.offsetHeight) / 2);
      }
      if (xAllowed && (leftToRight <= 0 || rightToLeft <= 0)) {
        win.scroll(win.scrollX + clientRect.left
                   - (win.innerWidth - elem.offsetWidth) / 2,
                   win.scrollY);
      }
    }
    if (win.parent !== win) {
      LH_scrollIntoViewIfNeeded(win.frameElement, centered);
    }
  }
function LH_scrollIntoViewIfNeeded ( elem , centered ) { centered = centered === undefined ? true : ! ! centered ; let win = elem . ownerDocument . defaultView ; let clientRect = elem . getBoundingClientRect ( ) ; let topToBottom = clientRect . bottom ; let bottomToTop = clientRect . top - win . innerHeight ; let leftToRight = clientRect . right ; let rightToLeft = clientRect . left - win . innerWidth ; let xAllowed = true ; let yAllowed = true ; if ( ( topToBottom > 0 || ! centered ) && topToBottom <= elem . offsetHeight ) { win . scrollBy ( 0 , topToBottom - elem . offsetHeight ) ; yAllowed = false ; } else if ( ( bottomToTop < 0 || ! centered ) && bottomToTop >= - elem . offsetHeight ) { win . scrollBy ( 0 , bottomToTop + elem . offsetHeight ) ; yAllowed = false ; } if ( ( leftToRight > 0 || ! centered ) && leftToRight <= elem . offsetWidth ) { if ( xAllowed ) { win . scrollBy ( leftToRight - elem . offsetWidth , 0 ) ; xAllowed = false ; } } else if ( ( rightToLeft < 0 || ! centered ) && rightToLeft >= - elem . offsetWidth ) { if ( xAllowed ) { win . scrollBy ( rightToLeft + elem . offsetWidth , 0 ) ; xAllowed = false ; } } if ( centered ) { if ( yAllowed && ( topToBottom <= 0 || bottomToTop >= 0 ) ) { win . scroll ( win . scrollX , win . scrollY + clientRect . top - ( win . innerHeight - elem . offsetHeight ) / 2 ) ; } if ( xAllowed && ( leftToRight <= 0 || rightToLeft <= 0 ) ) { win . scroll ( win . scrollX + clientRect . left - ( win . innerWidth - elem . offsetWidth ) / 2 , win . scrollY ) ; } } if ( win . parent !== win ) { LH_scrollIntoViewIfNeeded ( win . frameElement , centered ) ; } }
Scroll the document so that the element "elem" appears in the viewport.
Scroll the document so that the element " elem " appears in the viewport .
// We want to default to centering the element in the page, // so as to keep the context of the element. // The following are always from the {top, bottom, left, right} // of the viewport, to the {top, …} of the box. // Think of them as geometrical vectors, it helps. // The origin is at the top left. // We allow one translation on the x axis, // and one on the y axis. // Whatever `centered` is, the behavior is the same if the box is // (even partially) visible. // If we want it centered, and the box is completely hidden, // then we center it explicitly. // We are inside an iframe.