client-可视区域的宽高

1. Onresize事件
只要浏览器的大小改变,哪怕1像素,都会触动这个事件。
在这里插入图片描述

2.获取屏幕可视区域的封装

<script>
			/* console.log(document.body.clientHeight);
			console.log(document.body.clientWidth);
			console.log(document.documentElement.clientWidth);
			console.log(document.documentElement.clientHeight);
			console.log(window.innerHeight);
			console.log(window.innerWidth); */
			
			window.onresize=function(){
				console.log(1)
				document.title=client().width+"  "+client().height;
			}
			
			
			//获取屏幕可视区域的封装
			function client(){
				if(window.innerHeight!==undefined){
					return{
						"width":window.innerWidth,
						"height":window.innerHeight
					}
				}else if(document.compatMode==="CSS1Compat"){
					return{
						"width":document.documentElement.clientWidth,
						"height":document.documentElement.clientHeight
					}
				}else{
					return{
						"width":document.body.clientWidth,
						"height":document.body.clientHeight
					}
				}
			}
		</script>

3. 案例:根据浏览器可视区域大小,给定背景色
(基于响应式原理)

<body>
		<script src="jquery1.0.0.1.js"></script>
		<script>
			window.onresize=fn;
			fn();
			function fn(){
				if(client().width>960){
					document.body.style.backgroundColor="red";
				}else if(client().width>640){
					document.body.style.backgroundColor="blue";
				}else{
					document.body.style.backgroundColor="green";
				}
			}
		</script>
	</body>

4. 获得屏幕宽高(获得的是电脑分辨率)
window.screen.width
分辨率是屏幕图像的精密度,指显示器所能显示的像素有多少。

<script>
			window.onresize=function(){
				document.title=window.screen.width;
			}
		</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值