控制窗口的位置与大小

控制窗口位置

使用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()会将文档滚动到一个绝对位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无知的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值