控制窗口位置
使用window对象的screenLeft 和screenTop属性可以读取或设置窗口的位置,即相对于屏幕左边和上边的位置,但是FireFox支持使用window对象的screenX 和screenY属性进行相同操作
跨浏览器获取窗口左边和上边的位置
var leftPos = (typeof window.screenLeft == 'number') ? window.screenLeft : window.screenX;
var topPos = (typeof window.screenTop == 'number') ? window.screenTop : window.screenY;
console.log(leftPos+" "+topPos);
使用window对象的moveTo()和moveBy()可以移动窗口的位置,moveTo接收的时新位置的x和y的坐标,moveBy接收的是水平和垂直方向上移动的像素值
控制窗口大小
使用window对象的innerWidth、innerHeight、outWidth和outHeight这4个属性可以确定窗口位置大小
获取页面视图的大小
<script type="text/javascript">
var pageWidth = window.innerWidth;
var pageHeight = window.innerHeight;
if(typeof pageWidth != 'number') {
if(document.compatMode == 'CSS1Compat') {
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
} else {
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientWidth;
}
}
</script>
先将window.innerWidth和window.innerHeight的值分别赋值给 pageWidthh 和 pageHeight,然后检查 pageHeight中保存的是否是一个数值;如果不是通过检查document.compatMode属性确定页面是否处于标准模式。如果是,分别使用document.documentElement.clientWidth 和 document.documentElement.clientHeight的值,否则就使用 document.body.clientWidth 和 document.body.clientWidth的值
另外:
1、window对象定义了resizeBy()和resizeTo()方法,它们可以按照相对数量和绝对数量调整窗口的大小
2、方法scrollBy()会将窗口中的文档向左、右或向上、下滚动指定数量的像素
3、scrollTo()会将文档滚动到一个绝对位置。