怎么样获取element对应于整个页面的坐标
2008-04-10  作者:毛  同分类文章
description:
Gets the current position of an element based on page coordinates.
最简单的是用YUI。的 YAHOO.util.Dom.getXY("myElementId")。很好用。也很简单,如果你想知道简单点的底层点的,可以看下面我从犀牛书上抄来的。
javascript的犀牛书看了两边居然没有发现,还是因为我看的第四版低了。
先用document.getElementById()获取元素,然后传入下面的函数
// Get the X coordinate of the element e.
function getX(e) {
var x = 0; // Start with 0
while(e) { // Start at element e
x += e.offsetLeft; // Add in the offset
e = e.offsetParent; // And move up to the offsetParent
}
return x; // Return the total offsetLeft
}



function getY(element) {
var y = 0;
for(var e = element; e; e = e.offsetParent) // Iterate the offsetParents
y += e.offsetTop; // Add up offsetTop values

// Now loop up through the ancestors of the element, looking for
// any that have scrollTop set. Subtract these scrolling values from
// the total offset. However, we must be sure to stop the loop before
// we reach document.body, or we'll take document scrolling into account
// and end up converting our offset to window coordinates.
for(e = element.parentNode; e && e != document.body; e = e.parentNode)
if (e.scrollTop) y -= e.scrollTop; // subtract scrollbar values

// This is the Y coordinate with document-internal scrolling accounted for.
return y;
}



相关
简单仿discuz上传附件定位。
全选功能javascript
用javascript让页面的外部连接都在新窗口打开
proxy.flyy.info使用的最简单的ajax教程
各种颜色值
Yahoo! User Interface Library
fckeditor用javascript创建(结合ajax)
页面上所有的链接都在新窗口打开
选项卡效果
《Ajax基础》中文版