Skip to content

Commit 80bf043

Browse files
Merge pull request #140 from rodakd/fix-animation-in-scroll
Fix animation breaking inside scrolling containers
2 parents 51712c6 + 3a9339b commit 80bf043

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/index.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,33 @@ function raw(str: string): number {
336336
return Number(str.replace(/[^0-9.\-]/g, ""))
337337
}
338338

339+
/**
340+
* Get the scroll offset of elements
341+
* @param el - Element
342+
* @returns
343+
*/
344+
function getScrollOffset(el: Element) {
345+
let p = el.parentElement
346+
while (p) {
347+
if (p.scrollLeft || p.scrollTop) {
348+
return { x: p.scrollLeft, y: p.scrollTop }
349+
}
350+
p = p.parentElement
351+
}
352+
return { x: 0, y: 0 }
353+
}
354+
339355
/**
340356
* Get the coordinates of elements adjusted for scroll position.
341357
* @param el - Element
342358
* @returns
343359
*/
344360
function getCoords(el: Element): Coordinates {
345361
const rect = el.getBoundingClientRect()
362+
const { x, y } = getScrollOffset(el)
346363
return {
347-
top: rect.top + window.scrollY,
348-
left: rect.left + window.scrollX,
364+
top: rect.top + y,
365+
left: rect.left + x,
349366
width: rect.width,
350367
height: rect.height,
351368
}

0 commit comments

Comments
 (0)