MCPcopy
hub / github.com/ampproject/amphtml / scrollToElement_

Method scrollToElement_

src/service/navigation.js:655–677  ·  view source on GitHub ↗

* Scrolls the page to the given element. * @param {?Element} elem * @param {string} hash * @private

(elem, hash)

Source from the content-addressed store, hash-verified

653 * @private
654 */
655 scrollToElement_(elem, hash) {
656 // Scroll to the element if found.
657 if (elem) {
658 // The first call to scrollIntoView overrides browsers' default scrolling
659 // behavior. The second call insides setTimeout allows us to scroll to
660 // that element properly. Without doing this, the viewport will not catch
661 // the updated scroll position on iOS Safari and hence calculate the wrong
662 // scrollTop for the scrollbar jumping the user back to the top for
663 // failing to calculate the new jumped offset. Without the first call
664 // there will be a visual jump due to browser scroll. See
665 // https://github.com/ampproject/amphtml/issues/5334 for more details.
666 this.viewport_./*OK*/ scrollIntoView(elem);
667 Services.timerFor(this.ampdoc.win).delay(
668 () => this.viewport_./*OK*/ scrollIntoView(dev().assertElement(elem)),
669 1
670 );
671 } else {
672 dev().warn(
673 TAG,
674 `failed to find element with id=${hash} or a[name=${hash}]`
675 );
676 }
677 }
678
679 /**
680 * @param {string} url

Callers 1

handleHashNavigation_Method · 0.95

Calls 5

devFunction · 0.90
delayMethod · 0.80
assertElementMethod · 0.80
warnMethod · 0.80
scrollIntoViewMethod · 0.45

Tested by

no test coverage detected