Skip to content

Commit

Permalink
Fix scrollLeft and scrollTop to work with non-windows (#344)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Abbondanzo <[email protected]>
  • Loading branch information
Abbondanzo and Peter Abbondanzo authored Jun 14, 2023
1 parent 5c79fb7 commit 3db8947
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/comparisons/elements/scroll_left/modern.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
function scrollLeft(el, value) {
var win;
if (el.window === el) {
win = el;
} else if (el.nodeType === 9) {
win = el.defaultView;
}

if (value === undefined) {
return el.pageXOffset;
return win ? win.pageXOffset : el.scrollLeft;
}

if (win) {
win.scrollTo(value, win.pageYOffset);
} else {
if (el === window || el.nodeType === 9) {
el.scrollTo(value, el.pageYOffset);
} else {
el.pageXOffset = value;
}
el.scrollLeft = value;
}
}
19 changes: 13 additions & 6 deletions src/comparisons/elements/scroll_top/modern.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
function scrollTop(el, value) {
var win;
if (el.window === el) {
win = el;
} else if (el.nodeType === 9) {
win = el.defaultView;
}

if (value === undefined) {
return el.pageYOffset;
return win ? win.pageYOffset : el.scrollTop;
}

if (win) {
win.scrollTo(win.pageXOffset, value);
} else {
if (el === window || el.nodeType === 9) {
el.scrollTo(el.pageXOffset, value);
} else {
el.pageYOffset = value;
}
el.scrollTop = value;
}
}

0 comments on commit 3db8947

Please sign in to comment.