Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/core/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/pat/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions src/pat/scroll/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading