日期:2014-05-16  浏览次数:20428 次

如何获取iframe中对象的绝对位置
我现在是分开做,先求iframe的绝对位置,有没有一种通用的求对象绝对位置的办法,不管对象是在iframe还是其他什么东西里

------解决方案--------------------
function getElementPosition(element) {
var result = new Object();
result.x = 0;
result.y = 0;
result.width = 0;
result.height = 0;
if (element.offsetParent) {
result.x = element.offsetLeft;
result.y = element.offsetTop;
var parent = element.offsetParent;
while (parent) {
result.x += parent.offsetLeft;
result.y += parent.offsetTop;
var parentTagName = parent.tagName.toLowerCase();
if (parentTagName != "table " &&
parentTagName != "body " &&
parentTagName != "html " &&
parentTagName != "div " &&
parent.clientTop &&
parent.clientLeft) {
result.x += parent.clientLeft;
result.y += parent.clientTop;
}
parent = parent.offsetParent;
}
}
else if (element.left && element.top) {
result.x = element.left;
result.y = element.top;
}
else {
if (element.x) {
result.x = element.x;
}
if (element.y) {
result.y = element.y;
}
}
if (element.offsetWidth && element.offsetHeight) {
result.width = element.offsetWidth;
result.height = element.offsetHeight;
}
else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
result.width = element.style.pixelWidth;
result.height = element.style.pixelHeight;
}
return result;
}

var result = getElementPosition(ele);
alert(result.x + " " + result.y);
------解决方案--------------------
这个简单的也行
function Point(iX, iY)
{
this.x = iX;
this.y = iY;
}
function GetCoordinate(oTag)
{
var oTmp = oTag;
var pt = new Point(0,0);
do
{
pt.x += oTmp.offsetLeft;
pt.y += oTmp.offsetTop;
oTmp = oTmp.offsetParent;
} while(oTmp.tagName!= "BODY ");
return pt;
}
------解决方案--------------------
function getElementOffset(el)
{
var obj=el,offset=new Object(),x=0,y=0;
while(obj&&obj.tagName != "BODY ")
{
x+=obj.offsetLeft - obj.scrollLeft;
y+=obj.offsetTop - obj.scrollTop;
obj=obj.offsetParent;
}
offset.x=x;
offset.y=y;
return offset;
}