原生JS监听浏览器F11全屏按键方法

正文开始

https://blog.csdn.net/weixin_43080076/article/details/115361472

        if(!window.addF11Even) {
            window.addF11Even = 1;
            document.addEventListener('keydown', function(event) {
                if (event.key === 'F11') {
                    event.preventDefault();
                }
            });
            var callFull = function() {
                var maxIndex = 0;
                var maxBox = null;
                //取最上层的弹窗
                $.each(window['detailsBoxList'], function (n,box_) {
                    if(box_.zIndex > maxIndex) {
                        maxIndex = box_.zIndex;
                        maxBox = box_;
                    }
                });
                if(maxBox && maxBox.toggleFull) {
                    maxBox.toggleFull(1);
                }
            };
            var callCloseFull = function() {
                var maxIndex = 0;
                var maxBox = null;
                //取最上层的弹窗
                $.each(window['detailsBoxList'], function (n,box_) {
                    if(box_.zIndex > maxIndex) {
                        maxIndex = box_.zIndex;
                        maxBox = box_;
                    }
                });
                if(maxBox && maxBox.toggleFull) {
                    maxBox.toggleFull(0);
                }
            };
            $(document).on("keydown", function (e) {
                var e = event || window.event || arguments.callee.caller.arguments[0];
                if (e && e.keyCode == 122) {
                    //捕捉F11键盘动作
                    e.preventDefault(); //阻止F11默认动作
                    var el = document.documentElement;
                    var rfs =
                        el.requestFullScreen ||
                        el.webkitRequestFullScreen ||
                        el.mozRequestFullScreen ||
                        el.msRequestFullScreen; //定义不同浏览器的全屏API //执行全屏
                    if (typeof rfs != "undefined" && rfs) {
                        rfs.call(el);
                    } else if (typeof window.ActiveXObject != "undefined") {
                        var wscript = new ActiveXObject("WScript.Shell");
                        if (wscript != null) {
                            wscript.SendKeys("{F11}");
                        }
                    }

                    const weListenScreen = function () {
                        if (document.webkitIsFullScreen) {
                            //全屏后要执行的代码
                            callFull();
                        } else {
                            //退出全屏后执行的代码
                            document.removeEventListener("webkitfullscreenchange",weListenScreen);
                            callCloseFull();
                        }
                    };
                    const fuListenScreen = function () {
                        if (document.fullscreen) {
                            //全屏后要执行的代码
                            callFull();
                        } else {
                            //退出全屏后执行的代码
                            document.removeEventListener("fullscreenchange", fuListenScreen);
                            callCloseFull();
                        }
                    };
                    const moListenScreen = function () {
                        if (document.mozFullScreen) {
                            //全屏后要执行的代码
                            callFull();
                        } else {
                            //退出全屏后执行的代码
                            document.removeEventListener("mozfullscreenchange", moListenScreen);
                            callCloseFull();
                        }
                    };
                    const msListenScreen = function () {
                        if (document.msFullscreenElement) {
                            //全屏后要执行的代码
                            callFull();
                        } else {
                            //退出全屏后执行的代码
                            document.removeEventListener("msfullscreenchange", msListenScreen);
                            callCloseFull();
                        }
                    }; //监听不同浏览器的全屏事件,并件执行相应的代码
                    document.addEventListener("webkitfullscreenchange",weListenScreen,false);
                    document.addEventListener("fullscreenchange", fuListenScreen, false);
                    document.addEventListener("mozfullscreenchange", moListenScreen, false);
                    document.addEventListener("msfullscreenchange", msListenScreen, false);
                }
            });
        }

正文结束

js 正则替换返回值做回调函数 JS 实现全屏和退出全屏