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   }();

 

posted @ 2017-07-08 15:03  web_study  阅读(5624)  评论(0编辑  收藏  举报

哈哈