Skip to content

Commit

Permalink
Update chrome zoomed offset patch to use body
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Joel Steres committed Jun 12, 2017
1 parent e985896 commit c3e37d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ var DATA_DISPLAYCONTROLLER = 'displayController',
'mouseenter',
'mouseleave',
'contextmenu'
],
PATCH_DUMMY_ELEMENT_ID = 'chromeBugOffsetRef';
];

/**
* Session data
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div id="' + PATCH_DUMMY_ELEMENT_ID +
'" style="position: absolute; left: 0px; top: 0px; width: 1px; height: 1px; visibility: hidden"></div>').prependTo(document.body);
function activateChromeZoomedOffsetPatch() {
if (/Chrome\/[.0-9]*/.test(navigator.userAgent)) {
session.chromePatchRefElement = $(document.body);
}
}

Expand All @@ -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();
Expand Down

0 comments on commit c3e37d0

Please sign in to comment.