js代码如下:
<script>
// 初始触发
loadCenter($("#div_head"));
// 弹窗位置改变触发
window.οnresize=function(){
loadCenter($("#div_head"));
};
function loadCenter(el) {
// 可见窗口大小
var bodyW=document.documentElement.clientWidth;
var bodyH=document.documentElement.clientHeight;
// 本体大小宽度
var elW=el.width();
var elH=el.height();
// 计算偏移量
var leftL=(bodyW-elW)/2;
var topL=(bodyH-elH)/2;
$("#div_head").offset({left:leftL,top:topL});
}
</script>
效果图: