Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Fix live dev highlight being out of place in edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Gerber committed Oct 15, 2014
1 parent 50869ee commit fc096cf
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/LiveDevelopment/Agents/RemoteFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ function RemoteFunctions(experimental) {
}

// compute the screen offset of an element
function _screenOffset(element, key) {
var bounds = element.getBoundingClientRect();
if (key === "offsetLeft") {
return bounds.left + window.pageXOffset;
function _screenOffset(element) {
var elemBounds = element.getBoundingClientRect(),
body = window.document.body,
offsetTop,
offsetLeft;

if (window.getComputedStyle(body).position === "static") {
offsetLeft = elemBounds.left + window.pageXOffset;
offsetTop = elemBounds.top + window.pageYOffset;
} else {
return bounds.top + window.pageYOffset;
var bodyBounds = body.getBoundingClientRect();
offsetLeft = elemBounds.left - bodyBounds.left;
offsetTop = elemBounds.top - bodyBounds.top;
}
return { left: offsetLeft, top: offsetTop };
}

// set an event on a element
Expand Down Expand Up @@ -114,8 +122,9 @@ function RemoteFunctions(experimental) {
}

// compute the position on screen
var x = _screenOffset(this.element, "offsetLeft");
var y = _screenOffset(this.element, "offsetTop") + this.element.offsetHeight;
var offset = _screenOffset(this.element),
x = offset.left,
y = offset.top + this.element.offsetHeight;

// create the container
this.body = document.createElement("div");
Expand Down Expand Up @@ -239,9 +248,11 @@ function RemoteFunctions(experimental) {

highlight.className = HIGHLIGHT_CLASSNAME;

var offset = _screenOffset(element);

var stylesToSet = {
"left": _screenOffset(element, "offsetLeft") + "px",
"top": _screenOffset(element, "offsetTop") + "px",
"left": offset.left + "px",
"top": offset.top + "px",
"width": elementBounds.width + "px",
"height": elementBounds.height + "px",
"z-index": 2000000,
Expand Down

0 comments on commit fc096cf

Please sign in to comment.