From c3e37d09436974f629437216251a5a547377d818 Mon Sep 17 00:00:00 2001 From: Joel Steres Date: Wed, 2 Nov 2016 16:23:05 -0700 Subject: [PATCH] Update chrome zoomed offset patch to use body Rather than using an element absolutely positioned at 0,0, compute compensation value based on offset of body. This should be tested more rigorously but on the upside it does avoid further manipulating the DOM. --- src/core.js | 5 ++--- src/utility.js | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/core.js b/src/core.js index 45ae16f5..2d9f6295 100644 --- a/src/core.js +++ b/src/core.js @@ -35,8 +35,7 @@ var DATA_DISPLAYCONTROLLER = 'displayController', 'mouseenter', 'mouseleave', 'contextmenu' - ], - PATCH_DUMMY_ELEMENT_ID = 'chromeBugOffsetRef'; + ]; /** * Session data @@ -99,7 +98,7 @@ $.fn.powerTip = function(opts, arg) { return $.powerTip[opts].call(targetElements, targetElements, arg); } - injectChromePatchReferenceElement(); + activateChromeZoomedOffsetPatch(); // extend options options = $.extend({}, $.fn.powerTip.defaults, opts); diff --git a/src/utility.js b/src/utility.js index ef395cf4..703644ab 100644 --- a/src/utility.js +++ b/src/utility.js @@ -203,13 +203,17 @@ function countFlags(value) { } /** - * Conditionally insert reference element for use in Chrome zoomed offset patch + * Conditionally make reference for Chrome zoomed offset patch + * * Reference https://bugs.chromium.org/p/chromium/issues/detail?id=489206 + * Suggested patch calls for inserting an absolutely positioned element at 0,0. + * However, it appears that document.body serves equally well as a references + * and avoid needing to manipulate DOM. Beware offset behavior when body is not + * positioned at 0,0. */ -function injectChromePatchReferenceElement() { - if (/Chrome\/[.0-9]*/.test(navigator.userAgent) && !document.getElementById(PATCH_DUMMY_ELEMENT_ID)) { - session.chromePatchRefElement = $('').prependTo(document.body); +function activateChromeZoomedOffsetPatch() { + if (/Chrome\/[.0-9]*/.test(navigator.userAgent)) { + session.chromePatchRefElement = $(document.body); } } @@ -222,10 +226,10 @@ function injectChromePatchReferenceElement() { function getCompensatedOffset(element) { if (session.chromePatchRefElement) { var offset = element.offset(); - var rd = session.chromePatchRefElement[0].getBoundingClientRect(); + var r = session.chromePatchRefElement.offset(); return { - left: offset.left - (rd.left + window.pageXOffset), - top: offset.top - (rd.top + window.pageYOffset) + left: offset.left - r.left, + top: offset.top - r.top }; } return element.offset();