算法思路
- div.left = (视窗宽度-div的offsetWidth)/2
代码
var oDiv = document.getElementById('div1'); var clientW = document.documentElement.clientWidth; var clientH = document.documentElement.clientHeight; var divW = oDiv.offsetWidth; var divH = oDiv.offsetHeight; oDiv.style.left = (clientW - divW)/2 + 'px'; oDiv.style.top = (clientH - divH)/2 + 'px';
效果