From 4f563e6efb135e74454cf744eb9ce170ae7a4024 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 25 Sep 2019 08:36:30 +0300 Subject: [PATCH] account for parent padding, too --- .../offset_finder/offset_finder.service.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/services/offset_finder/offset_finder.service.js b/src/services/offset_finder/offset_finder.service.js index edee6412..1c45bb1a 100644 --- a/src/services/offset_finder/offset_finder.service.js +++ b/src/services/offset_finder/offset_finder.service.js @@ -4,17 +4,27 @@ export const findOffset = (child, parent, { top = 0, left = 0 } = {}, ignorePadd 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)) + const { topPadding, leftPadding } = findPadding(child) result.top += ignorePadding ? 0 : topPadding result.left += ignorePadding ? 0 : leftPadding } - if (child.offsetParent && (parent === window || parent.contains(child.offsetParent))) { + console.log('eee', parent, child.offsetParent) + if (child.offsetParent && (parent === window || parent.contains(child.offsetParent) || parent === child.offsetParent)) { return findOffset(child.offsetParent, parent, result, false) } else { + const { topPadding, leftPadding } = findPadding(parent) + result.top += topPadding + result.left += leftPadding return result } } + +const findPadding = (el) => { + const topPaddingStr = window.getComputedStyle(el)['padding-top'] + const topPadding = Number(topPaddingStr.substring(0, topPaddingStr.length - 2)) + const leftPaddingStr = window.getComputedStyle(el)['padding-left'] + const leftPadding = Number(leftPaddingStr.substring(0, leftPaddingStr.length - 2)) + + return { topPadding, leftPadding } +}