研究dhtmlx的combo组件,学习到获得元素的绝对坐标的方法
dhtmlXCombo.prototype.getPosition = function(oNode, pNode) {
if (!pNode)
var pNode = document.body
var oCurrentNode = oNode;
var iLeft = 0;
var iTop = 0;
while ((oCurrentNode) && (oCurrentNode != pNode)) {// .tagName!="BODY"){
iLeft += oCurrentNode.offsetLeft - oCurrentNode.scrollLeft;
iTop += oCurrentNode.offsetTop - oCurrentNode.scrollTop;
oCurrentNode = oCurrentNode.offsetParent;// isIE()?:oCurrentNode.parentNode;
}
if (pNode == document.body) {
if (_isIE) {
if (document.documentElement.scrollTop)
iTop += document.documentElement.scrollTop;
if (document.documentElement.scrollLeft)
iLeft += document.documentElement.scrollLeft;
} else if (!_isFF) {
iLeft += document.body.offsetLeft;
iTop += document.body.offsetTop;
}
}
return new Array(iLeft, iTop);
}
转载自
http://www.cnblogs.com/treeyh/archive/2007/12/06/985005.html
方法1:
function getAbsPoint(e)
{
var x = e.offsetLeft, y = e.offsetTop;
while(e=e.offsetParent)
{
x += e.offsetLeft;
y += e.offsetTop;
}
alert("x:"+x+","+"y:"+y);
}
方法2:
function getAbsPoint(obj)
{
var x,y;
oRect = obj.getBoundingClientRect();
x=oRect.left
y=oRect.top
alert("("+x+","+y+")")
}
JS中获得窗口属性的方法
1。获得屏幕的分辨率:
screen.width
screen.height
2。获得窗口大小:
document.body.clientWidth
document.body.clientHeight
3。获得窗口大小(包含Border、Scroll等元素)
document.body.offsetWidth
document.body.offsetHeight
另一种方法获得页面元素的坐标:
http://yexj00.5dm.cn/article.asp?id=120