1/** 2 * Scrolls a given parent element until a given child element is visible within the parent, without necessarily scrolling the entire page 3 * 4 * @param {HTMLElement} parent - Container element that is scrollable 5 * @param {HTMLElement} child - Element to make visible by scrolling the container element 6 * @return {boolean} True if the element was scrolled, false if it was already visible and didn't need to be scrolled 7 */ 8function scrollParentToChild(parent, child, animate = true) { 9 // Where is the parent on page? 10 const parentRect = parent.getBoundingClientRect(); 11 // What can you see? 12 const parentViewableArea = { 13 height: parent.clientHeight, 14 width: parent.clientWidth, 15 }; 16 17 // Where is the child? 18 const childRect = child.getBoundingClientRect(); 19 // Is the child viewable? 20 // @todo: add some functionality to determine if the target is x% visible (70%?). as it is now, if there is 1 pixel visible, it will not scroll 21 const isVerticallyViewable = (childRect.top >= parentRect.top) && (childRect.top <= parentRect.top + parentViewableArea.height); 22 const isHorizontallyViewable = (childRect.left >= parentRect.left) && (childRect.left <= parentRect.left + parentViewableArea.width); 23 24 // if you can't see the child try to scroll parent 25 if (!isVerticallyViewable) { 26 // scroll by offset relative to parent 27 const verticalOffset = (childRect.top + parent.scrollTop) - parentRect.top; 28 if (animate) { 29 jQuery(parent).animate({ scrollTop: verticalOffset }); 30 } else { 31 parent.scrollTop = verticalOffset; 32 } 33 return true; 34 } else if (!isHorizontallyViewable) { 35 const horizontalOffset = (childRect.left + parent.scrollLeft) - parentRect.left; 36 if (animate) { 37 jQuery(parent).animate({ scrollLeft: horizontalOffset }); 38 } else { 39 parent.scrollLeft = horizontalOffset; 40 } 41 return true; 42 } 43 return false; 44}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.