diff --git a/src/components/d-hover-box.js b/src/components/d-hover-box.js index 4735f1d..99a2d89 100644 --- a/src/components/d-hover-box.js +++ b/src/components/d-hover-box.js @@ -70,7 +70,7 @@ export class HoverBox extends T(HTMLElement) { } listen(element) { - // console.log(element) + // console.log(element); this.bindDivEvents(this); this.bindTriggerEvents(element); // this.style.display = "block"; @@ -128,7 +128,22 @@ export class HoverBox extends T(HTMLElement) { showAtNode(node) { // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop const bbox = node.getBoundingClientRect(); - this.show([node.offsetLeft + bbox.width, node.offsetTop + bbox.height]); + const offset = this.calcOffset(node); + this.show([offset.left + bbox.width, offset.top + bbox.height]); + } + + calcOffset(elem) { + let x = elem.offsetLeft; + let y = elem.offsetTop; + + // Traverse upwards until an `absolute` element is found or `elem` + // becomes null. + while (elem = elem.offsetParent && elem.style.position != 'absolute') { + x += elem.offsetLeft; + y += elem.offsetTop; + } + + return { left: x, top: y }; } hide() {