js获取控件坐标、位置定位以及宽高

获取控件Left绝对位置

function getAbsoluteLeft(id) {
	var el = document.getElementById(id);
	var left = el.offsetLeft - el.scrollLeft;
	while (el.offsetParent != null) {
		var parent = el.offsetParent;
		left += parent.offsetLeft - parent.scrollLeft;
		el = parent;
	}
	return left;
}

获取控件Top绝对位置

function getAbsoluteTop(id) {
	var el = document.getElementById(id);
	var top = el.offsetTop - el.scrollTop;
	while (el.offsetParent != null) {
		var parent = el.offsetParent;
		top += parent.offsetTop - parent.scrollTop;
		el = parent;
	}
	return top;
}

获取控件宽度

function getElementWidth(id) {
	var x = document.getElementById(id);
	return x.offsetWidth;
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×