Improved a lib function

This commit is contained in:
Alejandro Gallardo Escobar 2019-08-14 14:21:45 +02:00
parent 394815daa6
commit f6dfe023e9

View File

@ -405,10 +405,15 @@ export function debounce<T>(delay: number, fn: (...args: T[]) => void) {
* Retrieve the offset of an element relative to the page. * Retrieve the offset of an element relative to the page.
* @param el Node used to calculate the offset. * @param el Node used to calculate the offset.
*/ */
function getOffset(el: HTMLElement | null) { function getOffset(el: HTMLElement | null, parent?: HTMLElement) {
let x = 0; let x = 0;
let y = 0; let y = 0;
while (el && !Number.isNaN(el.offsetLeft) && !Number.isNaN(el.offsetTop)) { while (
el &&
!Number.isNaN(el.offsetLeft) &&
!Number.isNaN(el.offsetTop) &&
el !== parent
) {
x += el.offsetLeft - el.scrollLeft; x += el.offsetLeft - el.scrollLeft;
y += el.offsetTop - el.scrollTop; y += el.offsetTop - el.scrollTop;
el = el.offsetParent as HTMLElement | null; el = el.offsetParent as HTMLElement | null;
@ -548,8 +553,10 @@ export function addMovementListener(
// Store the difference between the cursor and // Store the difference between the cursor and
// the initial coordinates of the element. // the initial coordinates of the element.
lastX = element.offsetLeft; const elementOffset = getOffset(element, container);
lastY = element.offsetTop; lastX = elementOffset.left;
lastY = elementOffset.top;
// Store the mouse position. // Store the mouse position.
lastMouseX = e.pageX; lastMouseX = e.pageX;
lastMouseY = e.pageY; lastMouseY = e.pageY;