Ticket #300 (closed defect: duplicate)

Opened 8 months ago

Last modified 1 month ago

getElementPosition is wrong on elements inside an absolutely positioned element with border

Reported by: lalinsky@gmail.com Assigned to: somebody
Priority: normal Milestone:
Component: component1 Version:
Severity: normal Keywords:
Cc:

Description

Firefox seem to not include the border width of absolutely positioned elements in offsetLeft/offsetTop. You can see the behavior on:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head><title></title><script type="text/javascript" src="MochiKit.js"></script></head>
<body style="margin:0; padding:0;">

<div style="position:absolute; top:0px; left:0px; width:20px; border:solid 10px #000;">
<div id="foo" style="width:20px; height:10px; background:blue;"></div>
</div>

<br /><br /><br />
<div id="info"></div>

<script type="text/javascript">
connect(window, 'onload', function() { $('info').innerHTML = getElementPosition('foo').toString(); });
</script>

</body></html>

A simple fix, based on based on http://dev.rubyonrails.org/ticket/10149, that manually includes the border width:

                    c.x += parseInt(parent.style.borderTopWidth) || 0;
                    c.y += parseInt(parent.style.borderLeftWidth) || 0;

seems to help, but I'm not sure if it doesn't break anything else. I'll try to test in other browsers.

Change History

04/07/08 01:01:48 changed by cederberg@gmail.com

  • status changed from new to closed.
  • resolution set to duplicate.

I think this is a duplicate of #286, closing.

10/08/08 01:27:48 changed by cederberg@gmail.com

Funny thing about this patch is that it reverses x and y. The correct version should of course be:

c.x += parseInt(parent.style.borderLeftWidth) || 0;
c.y += parseInt(parent.style.borderTopWidth) || 0;

:-)