Skip to content

Commit 0233e00

Browse files
committed
Generalize toolbox positioning to support relative-offset context elements
1 parent b9e720f commit 0233e00

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

js/menu.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,19 @@ let referencePane = {
875875
},
876876
};
877877

878+
function getAbsoluteOffset(el, offsetName) {
879+
// "offsetTop" => "borderTopWidth", "offsetLeft" => "borderLeftWidth"
880+
const borderWidthName = offsetName.replace(/offset(.*)/, 'border$1Width');
881+
let d = el[offsetName];
882+
for (let parent = el.offsetParent; parent; parent = parent.offsetParent) {
883+
const parentStyle = getComputedStyle(parent);
884+
const parentBorderWidth = parentStyle[borderWidthName];
885+
const parentBorderPx = parseFloat(parentBorderWidth);
886+
d += (parentBorderPx || 0) + parent[offsetName];
887+
}
888+
return d;
889+
}
890+
878891
let Toolbox = {
879892
init() {
880893
this.$outer = document.createElement('div');
@@ -914,8 +927,8 @@ let Toolbox = {
914927
this.entry = entry;
915928
this.$pinLink.textContent = menu._pinnedIds[entry.id] ? 'Unpin' : 'Pin';
916929
this.$outer.classList.add('active');
917-
this.top = el.offsetTop - this.$outer.offsetHeight;
918-
this.left = el.offsetLeft - 10;
930+
this.top = getAbsoluteOffset(el, 'offsetTop') - this.$outer.offsetHeight;
931+
this.left = getAbsoluteOffset(el, 'offsetLeft') - 10;
919932
this.$outer.setAttribute('style', 'left: ' + this.left + 'px; top: ' + this.top + 'px');
920933
this.updatePermalink();
921934
this.updateReferences();

0 commit comments

Comments
 (0)