function scrollImg() {
var posX, posY;
if (window.innerHeight) {
posX = window.pageXOffset;
posY = window.pageYOffset;
}
else if (document.documentElement && document.documentElement.scrollTop) {
posX = document.documentElement.scrollLeft;
posY = document.documentElement.scrollTop;
}
else if (document.body) {
posX = document.body.scrollLeft;
posY = document.body.scrollTop;
}
var ad = document.getElementById("OrderConfirm");
ad.style.top = (posY + 150) + "px";
ad.style.left = (posX + 250) + "px";
setTimeout("scrollImg()", 100); } window.onscroll = scrollImg;
//居中显示 开始
var ad_Confirm = document.getElementById("层ID");
ad_Confirm.style.left = parseInt((document.documentElement.scrollWidth - ad_Confirm.offsetWidth) / 2) +
document.documentElement.scrollLeft + "px";
ad_Confirm.style.top = Math.abs(parseInt((document.documentElement.clientHeight - ad_Confirm.offsetHeight) / 2)) +
document.documentElement.scrollTop + "px";
//居中显示 结束
<style type="text/css">
<!--
div {
position:absolute;
top:50%;
left:50%;
margin:-150px 0 0 -200px;
width:400px;
height:300px;
border:1px solid #008800;
}
-->
</style>
<div>让层垂直居中于浏览器窗口</div>
其实解决的思路是这样的:首们需要position:absolute;绝对定位。而层的定位点,使用外补丁margin负值的方法。负值的大小为层自身宽度高度除以二。
如:一个层宽度是400,高度是300。使用绝对定位距离上部与左部都设置成50%。而margin-top的值为-150。margin-left的值为-200。这样我们就实现了层垂直居中于浏览器的样式编写
注:上面的是一般用于垂直居中与浏览器,想解决div垂直居中与外层div的话,只需要给外层div加上position:relative;就好了;记住,外层div要设定高度和宽度