Skip to content

Fixup/compensate for chrome zoomed offset bug #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ var session = {
windowWidth: 0,
windowHeight: 0,
scrollTop: 0,
scrollLeft: 0
scrollLeft: 0,
chromePatchRefElement: null
};

/**
Expand Down Expand Up @@ -97,6 +98,8 @@ $.fn.powerTip = function(opts, arg) {
return $.powerTip[opts].call(targetElements, targetElements, arg);
}

activateChromeZoomedOffsetPatch();

// extend options
options = $.extend({}, $.fn.powerTip.defaults, opts);

Expand Down
6 changes: 3 additions & 3 deletions src/placementcalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function PlacementCalculator() {
* @return {Object} An object with the top,left position values.
*/
function getHtmlPlacement(element, placement) {
var objectOffset = element.offset(),
var objectOffset = getCompensatedOffset(element),
objectWidth = element.outerWidth(),
objectHeight = element.outerHeight(),
left,
Expand Down Expand Up @@ -217,10 +217,10 @@ function PlacementCalculator() {
}
}

return {
return compensateForZoomBug({
top: coords.y + session.scrollTop,
left: coords.x + session.scrollLeft
};
});
}

// expose methods
Expand Down
54 changes: 53 additions & 1 deletion src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function isMouseOver(element) {
// methods do not work with SVG elements
// compute width/height because those properties do not exist on the object
// returned by getBoundingClientRect() in older versions of IE
var elementPosition = element.offset(),
var elementPosition = getCompensatedOffset(element),
elementBox = element[0].getBoundingClientRect(),
elementWidth = elementBox.right - elementBox.left,
elementHeight = elementBox.bottom - elementBox.top;
Expand Down Expand Up @@ -201,3 +201,55 @@ function countFlags(value) {
}
return count;
}

/**
* 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 activateChromeZoomedOffsetPatch() {
var style;
if (!session.chromePatchRefElement && /Chrome\/[.0-9]*/.test(navigator.userAgent)) {
session.chromePatchRefElement = $(document.body);
style = {
top: 0,
left: 0,
position: 'absolute',
display: 'hidden',
height: '1px',
margin: 0,
width: '1px',
zIndex: -1
};
session.chromePatchRefElement = $('<div/>').css(style).appendTo($(document.body));
}
}

/**
* Compensate for the Chrome getBoundingClientRect bug when zoomed.
* Reference https://bugs.chromium.org/p/chromium/issues/detail?id=489206
* @param {jQuery} element The element that the tooltip should target.
* @return {Offsets} The top, left offsets relative to the document.
*/
function getCompensatedOffset(element) {
return compensateForZoomBug(element.offset());
}

/**
* Compensate for the Chrome measurement bug when zoomed.
* @param {object} coords Coordinates to compensate for if zoomed on chrome
* @return {Offsets} The top, left offsets relative to the document.
*/
function compensateForZoomBug(coords) {
if (session.chromePatchRefElement) {
var r = session.chromePatchRefElement.offset();
coords.top -= r.top;
coords.left -= r.left;
return coords;
}
return coords;
}