diff --git a/src/core/dom.js b/src/core/dom.js index 01c9ef8b8..053d81e06 100644 --- a/src/core/dom.js +++ b/src/core/dom.js @@ -364,9 +364,12 @@ const get_relative_position = (el, reference_el = document.body) => { // the relative position of the target. // In case of a scroll container of window, we do not have // getBoundingClientRect method, so get the body instead. - if (reference_el === window) { + if (reference_el === window || document) { reference_el = document.body; } + if (el === window || document) { + el = document.body; + } // Calculate absolute [ยน] position difference between // scroll_container and scroll_target. @@ -407,6 +410,14 @@ const get_relative_position = (el, reference_el = document.body) => { * @param {string} [direction="top"] - The direction to scroll to. Can be either "top", "left" or "both". */ const scroll_to_element = (el, scroll_container, offset = 0, direction = "top") => { + // Normalize el and scroll_container + if (el === window || document) { + el = document.body; + } + if (scroll_container === window || document) { + scroll_container = document.body; + } + // Get the position of the element relative to the scroll container. const position = get_relative_position(el, scroll_container); diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index e396c4c12..5081397a4 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -595,7 +595,8 @@ const inject = { ) { $title = sources$[sources$.length - 1]; } - cfgs.forEach((cfg, idx1) => { + + for (const [idx1, cfg] of cfgs.entries()) { const perform_inject = () => { if (cfg.target !== "none") { for (const target of cfg.$target) { @@ -615,7 +616,7 @@ const inject = { } else { perform_inject(); } - }); + } if (cfgs[0].nextHref && $el.is("a")) { // In case next-href is specified the anchor's href will // be set to it after the injection is triggered. diff --git a/src/pat/scroll/scroll.js b/src/pat/scroll/scroll.js index 7e8c248ab..c5d70f103 100644 --- a/src/pat/scroll/scroll.js +++ b/src/pat/scroll/scroll.js @@ -52,12 +52,15 @@ class Pattern extends BasePattern { await utils.timeout(this.options.delay); } - const target = this.get_target(); - const scroll_container = dom.find_scroll_container( - target.parentElement, - this.options.direction === "top" ? "y" : "x", - window - ); + const target = this.get_target() || document.body; + const scroll_container = + target === document.body + ? document.body + : dom.find_scroll_container( + target.parentElement, + this.options.direction === "top" ? "y" : "x", + window + ); // Set/remove classes on beginning and end of scroll this.el.classList.add("pat-scroll-animated");