日期:2014-05-16  浏览次数:20522 次

js将对象居中函数
//将对象居中显示
function moveMiddle(obj){
	if(!obj) return ;
	
	obj.style.display = "block";
	
	var screenWidth = document.documentElement.clientWidth;
	var screenHeight = document.documentElement.clientHeight;
	var scrollPosY = 0;
	var scrollPosX = 0

	if(typeof window.pageYOffset != 'undefined'){
		scrollPosY = window.pageYOffset;
	}else if(typeof document.documentElement != 'undefined'){
		scrollPosY = document.documentElement.scrollTop;
	}
	if(typeof window.pageXOffset != 'undefined'){
		scrollPosX = window.pageXOffset;
	}else if(typeof document.documentElement != 'undefined'){
		scrollPosX = document.documentElement.scrollLeft ;
	}
	
	var divWidth = obj.offsetWidth;
	var divHeight = obj.offsetHeight;

	var divLeft = parseInt((screenWidth - divWidth) / 2);
	var divTop = parseInt((screenHeight - divHeight) / 2);

	divTop = divHeight > screenHeight ? 10 : divTop + scrollPosY;
	divLeft = divWidth > screenWidth ? 10 : divLeft + scrollPosX;

	obj.style.left = divLeft + "px";
	obj.style.top = divTop + "px";
	
}
?