From daa0e284c32cc8c97006e3a1701ab96ddfabfd30 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 25 Sep 2019 00:21:55 +0300 Subject: [PATCH] forgotten file --- .../offset_finder/offset_finder.service.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/services/offset_finder/offset_finder.service.js diff --git a/src/services/offset_finder/offset_finder.service.js b/src/services/offset_finder/offset_finder.service.js new file mode 100644 index 00000000..edee6412 --- /dev/null +++ b/src/services/offset_finder/offset_finder.service.js @@ -0,0 +1,20 @@ +export const findOffset = (child, parent, { top = 0, left = 0 } = {}, ignorePadding = true) => { + const result = { + top: top + child.offsetTop, + left: left + child.offsetLeft + } + if (!ignorePadding && child !== window) { + const topPaddingStr = window.getComputedStyle(child)['padding-top'] + const topPadding = Number(topPaddingStr.substring(0, topPaddingStr.length - 2)) + const leftPaddingStr = window.getComputedStyle(child)['padding-left'] + const leftPadding = Number(leftPaddingStr.substring(0, leftPaddingStr.length - 2)) + result.top += ignorePadding ? 0 : topPadding + result.left += ignorePadding ? 0 : leftPadding + } + + if (child.offsetParent && (parent === window || parent.contains(child.offsetParent))) { + return findOffset(child.offsetParent, parent, result, false) + } else { + return result + } +}