js函数收藏

获取元素相对于页面的偏移值
_getElementRect = function(e) {
	var bounds;
	try { bounds = e.getBoundingClientRect(); } // this can fail on disconnected DOM elements in IE9
	catch (err) { bounds = {top: e.offsetTop, left: e.offsetLeft, width:e.offsetWidth, height:e.offsetHeight}; }

	var offX = (window.pageXOffset || document.scrollLeft || 0) - (document.clientLeft || document.body.clientLeft || 0);
	var offY = (window.pageYOffset || document.scrollTop || 0) - (document.clientTop  || document.body.clientTop  || 0);

	var styles = window.getComputedStyle ? getComputedStyle(e,null) : e.currentStyle; // IE <9 compatibility.
	var padL = parseInt(styles.paddingLeft)+parseInt(styles.borderLeftWidth);
	var padT = parseInt(styles.paddingTop)+parseInt(styles.borderTopWidth);
	var padR = parseInt(styles.paddingRight)+parseInt(styles.borderRightWidth);
	var padB = parseInt(styles.paddingBottom)+parseInt(styles.borderBottomWidth);

	// note: in some browsers bounds properties are read only.
	return {
		left: bounds.left+offX+padL,
		right: bounds.right+offX-padR,
		top: bounds.top+offY+padT,
		bottom: bounds.bottom+offY-padB
	}
}





你可能感兴趣的:(js函数收藏)