From 8482b8a7b4e5d2241aeadaa5796d0ca764c3d912 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Thu, 1 Sep 2016 11:01:41 +0200 Subject: [PATCH] take scroll into account in absolute position calculation --- src/helpers.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/helpers.js b/src/helpers.js index ed87092d..efd207fe 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -106,12 +106,16 @@ Helpers.getAbsolutePosition = function (element) { if (!(element instanceof Element)) { return {x: 0, y: 0}; } + var css; + var origElement = element; + try { css = getComputedStyle(element); } catch (e) { return {x: 0, y: 0}; } + if (!css) { return {x: 0, y: 0}; } @@ -131,6 +135,14 @@ Helpers.getAbsolutePosition = function (element) { element = element.offsetParent; } + element = origElement; + + while (element.parentNode && !(element instanceof HTMLBodyElement)) { + x -= element.scrollLeft || 0; + y -= element.scrollTop || 0; + element = element.parentNode; + } + return {x: x, y: y}; };