浏览器视口宽度高度的封装

  视口是红色线包围的部分

    // 视口宽度与高度的封装
    // 视口是指浏览器中,用于显示网页文档内容部分
    function client() {
        if(window.innerWidth !=null) {//兼容 IE9以上 及 现代的浏览器
            return {//花括号一定不能换行到下一行,否则返回空
            // return 的内容一定要和return在同一行,不能换行
                width : window.innerWidth,
                height : window.innerHeight
            }
        }else if(document.compatMode === "CSS1Compat") {//标准浏览器,有<!DOCTYPE html>声明的
            return {
                width : document.documentElement.clientWidth,
                height : document.documentElement.clientHeight
            }
        }else {//怪异浏览器,没有<!DOCTYPE html>声明的
            return {
                width : document.body.clientWidth,
                height : document.body.clientHeight
            }
            
        }
    }
原文地址:https://www.cnblogs.com/darkterror/p/6214839.html