Skip to content

Instantly share code, notes, and snippets.

@TikiTDO
Last active August 29, 2015 14:18
Show Gist options
  • Save TikiTDO/2add417d91eefe4bed46 to your computer and use it in GitHub Desktop.
Save TikiTDO/2add417d91eefe4bed46 to your computer and use it in GitHub Desktop.
Fixes the jsPlumb getOffset function to ignore parent element scroll offsets.
// Fixes the jsPlumb getOffset function to ignore parent element scroll offsets.
// Note - This is a hack. It may have unintended side-effects including
// breaking your page, your computer, and/or your universe.
jsPlumbInstance.prototype.getOffset = function(el, relativeToRoot) {
var current_element, container, out, parent_element;
// Initialization
current_element = jsPlumb.getDOMElement(el);
container = this.getContainer();
out = {
left: current_element.offsetLeft,
top: current_element.offsetTop
};
// Walk up the tree until exhausing the parent elements for offset calculation
parent_element = (relativeToRoot || (container != null && current_element.offsetParent != container)) ? current_element.offsetParent : null;
while (parent_element != null) {
// Include the parent element offset in the total offset calculations
out.left += parent_element.offsetLeft;
out.top += parent_element.offsetTop;
// Some sort of scroll thing
if (!relativeToRoot) {
if (parent_element != null && parent_element.scrollTop > 0 || parent_element.scrollLeft > 0) {
var p = this.getStyle(current_element, "position");
if (p !== "fixed") {
//out.left -= parent_element.scrollLeft;
//out.top -= parent_element.scrollTop;
}
}
}
// Control how far up the tree to walk
if (relativeToRoot) {
// Walk up to root
parent_element = parent_element.offsetParent;
} else {
// Walk up to container
parent_element = parent_element.offsetParent == container ? null : parent_element.offsetParent;
}
}
// if container is scrolled and the element (or its offset parent) is not absolute or fixed, adjust accordingly.
if (container != null && !relativeToRoot && (container.scrollTop > 0 || container.scrollLeft > 0)) {
var parent_position = current_element.offsetParent != null ? this.getStyle(current_element.offsetParent, "position") : "static",
current_position = this.getStyle(current_element, "position");
if (current_position !== "absolute" && current_position !== "fixed" && parent_position !== "absolute" && parent_position != "fixed") {
out.left -= container.scrollLeft;
out.top -= container.scrollTop;
}
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment