File tree 1 file changed +19
-2
lines changed
1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -336,16 +336,33 @@ function raw(str: string): number {
336
336
return Number ( str . replace ( / [ ^ 0 - 9 . \- ] / g, "" ) )
337
337
}
338
338
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
+
339
355
/**
340
356
* Get the coordinates of elements adjusted for scroll position.
341
357
* @param el - Element
342
358
* @returns
343
359
*/
344
360
function getCoords ( el : Element ) : Coordinates {
345
361
const rect = el . getBoundingClientRect ( )
362
+ const { x, y } = getScrollOffset ( el )
346
363
return {
347
- top : rect . top + window . scrollY ,
348
- left : rect . left + window . scrollX ,
364
+ top : rect . top + y ,
365
+ left : rect . left + x ,
349
366
width : rect . width ,
350
367
height : rect . height ,
351
368
}
You can’t perform that action at this time.
0 commit comments