日期:2014-05-17  浏览次数:20665 次

JS获得HTML元素位置
//获得元素位置
	function getAbsolutePosition(obj) {
        	position = new Object();
        	position.x = 0;
        	position.y = 0;
        	position.h = 0;
        	position.w = 0;
        	var tempobj = obj;
        	if (tempobj.offsetHeight + '' != 'undefined') { position.h = tempobj.offsetHeight; }
        	if (tempobj.offsetWidth + '' != 'undefined') { position.w = tempobj.offsetWidth; }
        	while (tempobj != null && tempobj != document.body) {
        	    position.x += tempobj.offsetLeft + tempobj.clientLeft;
         	   position.y += tempobj.offsetTop + tempobj.clientTop;
        	    tempobj = tempobj.offsetParent
       		}
        	return position;
   	}

//使用
var ask = getAbsolutePosition(xxx);
alert(ask.x);   //横坐标
alert(ask.y);   //纵坐标
alert(ask.h);   //高
alert(ask.w);   //宽