Skip to content

Commit 4654a54

Browse files
committed
Bug: Fix issue with scrollTop/left on globals like body. Fix offset conditions for relativeTo
1 parent 29a1600 commit 4654a54

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

packages/query/src/query.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ export class Query {
329329
}
330330

331331
is(selector) {
332+
if (this.length == 0) {
333+
return false;
334+
}
332335
const filteredElements = Array.from(this).filter((el) => {
333336
if (typeof selector === 'string') {
334337
return el.matches && el.matches(selector);
@@ -1343,13 +1346,27 @@ export class Query {
13431346
}
13441347

13451348
scrollLeft(value) {
1346-
const el = (this.isGlobal && this.isBrowser) ? this.chain(document.documentElement) : this;
1347-
return el.prop('scrollLeft', value);
1349+
// special case <body> for window scroll
1350+
if (this.isGlobal || this.isBrowser || this.is('body')) {
1351+
if (value !== undefined) {
1352+
window.scroll(value, scrollY);
1353+
return this;
1354+
}
1355+
return window.scrollX;
1356+
}
1357+
return this.prop('scrollLeft', value);
13481358
}
13491359

13501360
scrollTop(value) {
1351-
const el = (this.isGlobal && this.isBrowser) ? this.chain(document.documentElement) : this;
1352-
return el.prop('scrollTop', value);
1361+
// special case <body> for window scroll
1362+
if (this.isGlobal || this.isBrowser || this.is('body')) {
1363+
if (value !== undefined) {
1364+
window.scroll(window.scrollX, value);
1365+
return this;
1366+
}
1367+
return window.scrollY;
1368+
}
1369+
return this.prop('scrollTop', value);
13531370
}
13541371

13551372
clone() {
@@ -1728,7 +1745,7 @@ export class Query {
17281745
const parents = this.map((el) => {
17291746
let current = el.parentNode;
17301747
while (current) {
1731-
if (current instanceof Element) {
1748+
if (current instanceof Element && current !== document.body) {
17321749
const style = window.getComputedStyle(current);
17331750

17341751
// Check overflow
@@ -2099,8 +2116,8 @@ export class Query {
20992116
outerHeight,
21002117
marginHeight,
21012118
// Scroll
2102-
scrollTop: el.scrollTop,
2103-
scrollLeft: el.scrollLeft,
2119+
scrollTop: el.scrollY || el.scrollTop,
2120+
scrollLeft: el.scrollX || el.scrollLeft,
21042121
scrollHeight: el.scrollHeight,
21052122
scrollWidth: el.scrollWidth,
21062123
// Box Model Details
@@ -2228,7 +2245,6 @@ export class Query {
22282245
}
22292246
}
22302247
}
2231-
22322248
return returnDetails ? details : details.intersects;
22332249
});
22342250
}).flat();
@@ -2307,8 +2323,8 @@ export class Query {
23072323
if (relativeTo) {
23082324
const relativeRect = $relative.dimensions();
23092325
relativeCoords = {
2310-
top: round(elRect.top - relativeRect.top - relativeRect.box.border.left),
2311-
left: round(elRect.left - relativeRect.left - relativeRect.box.border.left),
2326+
top: round(elRect.top - relativeRect.top - relativeRect.box.border.left - relativeRect.scrollTop),
2327+
left: round(elRect.left - relativeRect.left - relativeRect.box.border.left - relativeRect.scrollLeft),
23122328
};
23132329
if (!isSetter && type === 'relative') {
23142330
return relativeCoords;

0 commit comments

Comments
 (0)