From 1b197741fa0f2947f1e52a0f0545186da55dfa58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 2 Jun 2011 20:43:54 +0100 Subject: [PATCH] Add support for calculation of cumulative positioned offsets and use it to correctly determine visibility and event positions within canvases nested in relative divs. --- src/browser/DomElement.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/browser/DomElement.js b/src/browser/DomElement.js index f5c73431..132246d8 100644 --- a/src/browser/DomElement.js +++ b/src/browser/DomElement.js @@ -15,12 +15,15 @@ */ var DomElement = new function() { - function cumulate(el, name, parent) { + function cumulate(el, name, parent, positioned) { var left = name + 'Left', top = name + 'Top', x = 0, y = 0; - while (el) { + // If we're asked to calculate positioned offset, stop at any + // parent element that has relative or absolute position. + while (el && (!positioned + || !/^(relative|absolute)$/.test(el.style.position))) { x += el[left] || 0; y += el[top] || 0; el = el[parent]; @@ -29,8 +32,8 @@ var DomElement = new function() { } return { - getOffset: function(el, scroll) { - var point = cumulate(el, 'offset', 'offsetParent'); + getOffset: function(el, positioned, scroll) { + var point = cumulate(el, 'offset', 'offsetParent', positioned); return scroll ? point.subtract(cumulate(el, 'scroll', 'parentNode')) : point; @@ -40,8 +43,8 @@ var DomElement = new function() { return Size.create(el.offsetWidth, el.offsetHeight); }, - getBounds: function(el, scroll) { - return new Rectangle(DomElement.getOffset(el, scroll), + getBounds: function(el, positioned, scroll) { + return new Rectangle(DomElement.getOffset(el, positioned, scroll), DomElement.getSize(el)); }, @@ -59,7 +62,7 @@ var DomElement = new function() { // See if the scrolled bounds intersect with the windows rectangle // which always starts at 0, 0 return new Rectangle([0, 0], DomElement.getWindowSize()) - .intersects(DomElement.getBounds(el, true)); + .intersects(DomElement.getBounds(el, false, true)); } }; };