以前都是这里找那里找,现在自己修改好了一个在各种浏览器上兼容的js代码:
div:
<div id=’animation’ style=’display:none;position:fixed;width:270px;height:220px;background-color:#fff;z-index:9999;’></div>
js:
var winWidth = 0;
var winHeight = 0;
function findDimensions(){//函数:获取尺寸
//获取宽度
if(window.innerWidth)
winWidth = window.innerWidth;
else if((document.body) && (document.body.clientWidth))
winWidth = document.body.clientWidth;
//获取高度
if(window.innerHeight)
winHeight = window.innerHeight;
else if((document.body) && (document.body.clientHeight))
winHeight = document.body.clientHeight;
//对document body深入检测,获取窗口大小
if(document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth){
winWidth = document.documentElement.clientWidth;
winHeight = document.documentElement.clientHeight;
}
//alert(winWidth+’,'+winHeight);
changePosition(‘animation’, winWidth, winHeight);
}
findDimensions();
// 调用函数,获取数值
window.onresize=findDimensions;
function changePosition(id, width, height) {//函数:改变DIV位置
document.getElementById(id).style.display = ‘block’;
document.getElementById(id).style.left = (width – 273) + ‘px’;
document.getElementById(id).style.top = (height – 223) + ‘px’;
}