Is this doable in either IE7 or Firefox?
views:
5178answers:
5
+2
A:
Try the dimensions jQuery plugin. See this demo.
$('#myelement.').offset();
Pistos
2008-10-17 11:02:35
A:
You could subtract the div's offsetTop from the document.body.scrollTop
This seems to work on IE7 and FF3, but on a very simple page. I haven't checked with nested DIVs.
rslite
2008-10-17 11:04:01
+5
A:
You can do it in both - get the position relative to the document, then subtract the scroll position.
var e = document.getElementById('xxx');
var position = {x:0,y:0};
while (e)
{
position.x += e.offsetLeft;
position.y += e.offsetTop;
e = e.offsetParent;
}
if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft))
{
offset.x -= document.documentElement.scrollLeft;
offset.y -= document.documentElement.scrollTop;
}
else if (document.body && (document.body.scrollTop || document.body.scrollLeft))
{
offset.x -= document.body.scrollLeft;
offset.y -= document.bodt.scrollTop;
}
else if (window.pageXOffset || window.pageYOffset)
{
offset.x -= window.pageXOffset;
offset.y -= window.pageYOffset;
}
alert(offset.x + '\n' + offset.y);
Greg
2008-10-17 11:04:51
A:
Using Prototype it would be:
$('divname').viewportOffset.top
$('divname').viewportOffset.left
Diodeus
2008-10-17 12:28:34
A:
In IE and Firefox 3, you can use getBoundingClientRect for this; no framework necessary.
But, yes, you should use a framework if you need to support other browsers as well.
savetheclocktower
2008-10-17 20:44:06