js兼容性——获取当前浏览器窗口的宽高

通过onresize事件

 1 window.onresize = function () {
 2     document.title = client().width + "    "+ client().height;
 3 }
 4 
 5 
 6  //获取屏幕可视区域的宽高
 7 function client(){
 8    if(window.innerHeight !== undefined){
 9        return {
10             "width": window.innerWidth,
11             "height": window.innerHeight
12         }
13     }else if(document.compatMode === "CSS1Compat"){
14             return {
15                 "width": document.documentElement.clientWidth,
16                 "height": document.documentElement.clientHeight
17             }
18     }else{
19        return {
20             "width": document.body.clientWidth,
21              "height": document.body.clientHeight
22        }
23     }
24}
25 
26 二、获取浏览器宽度和高度-使用懒函数优化
27 window.onresize = function() {                                
28     document.title = client().width + "    " + client().height;
29}
30 
31  //获取屏幕可视区域的宽高
32    !function() {
33        if(window.innerHeight !== undefined) {
34             client = function() {
35                 return {
36                    "width": window.innerWidth,
37                    "height": window.innerHeight
38                 }
39              }
40        } else if(document.compatMode === "CSS1Compat") {
41              client = function() {
42                  return {
43                     "width": document.documentElement.clientWidth,
44                     "height": document.documentElement.clientHeight
45                   }
46               }
47 
48        } else {
49              client = function() {
50                  return {
51                      "width": document.body.clientWidth,
52                      "height": document.body.clientHeight
53                   }
54              }
55 
56         }
57   }();

 

转载于:https://www.cnblogs.com/web-wjg/p/7136899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值