js全屏问题

判断全屏

function isFullscreen(){
    return document.fullscreenElement    ||
    document.msFullscreenElement  ||
    document.mozFullScreenElement ||
    document.webkitFullscreenElement || false;
}

 判断全屏,这个是重点(按了F11或ESC的情况,上面的判断不了)

        function isFullscreenForNoScroll(){
            var explorer = window.navigator.userAgent.toLowerCase();
            if(explorer.indexOf('chrome')>0){//webkit
                if (document.body.scrollHeight === window.screen.height && document.body.scrollWidth === window.screen.width) {
                    alert("全屏");
                } else {
                    alert("不全屏");
                }
            }else{//IE 9+  fireFox
                if (window.outerHeight === window.screen.height && window.outerWidth === window.screen.width) {
                    alert("全屏");
                } else {
                    alert("不全屏");
                }
            }
        }

全屏 

        /**
         * [setFullscreen 设置全屏]
         * @param {domElement} element [DOM节点对象(可选)]
         */
        function setFullscreen(element) {
            var el = element instanceof HTMLElement ? element : document.documentElement;
            var rfs = el.requestFullscreen       || 
                      el.webkitRequestFullscreen || 
                      el.mozRequestFullScreen    || 
                      el.msRequestFullscreen;
            if (rfs) {
                rfs.call(el);
            } else if (window.ActiveXObject) {
                var ws = new ActiveXObject("WScript.Shell");
                ws && ws.SendKeys("{F11}");
            }
        }

 退出全屏

 

        /**
         * [exitFullscreen 退出全屏]
         */
        function exitFullscreen(){
            var efs = document.exitFullscreen       || 
                      document.webkitExitFullscreen || 
                      document.mozCancelFullScreen  || 
                      document.msExitFullscreen;
            if (efs) {
                efs.call(document);
            } else if (window.ActiveXObject) {
                var ws = new ActiveXObject("WScript.Shell");
                ws && ws.SendKeys("{F11}");
            }
        }

 

你可能感兴趣的:(JavaScript)