全屏/退出全屏/监听全屏或退出全屏状态(jquery/原生js/快捷方式)

21 篇文章 0 订阅
20 篇文章 0 订阅

方案一

	//js
    switchFullScreen() {
        const docElm = document.documentElement;
        if (!this.isFullScreen) {
            if (docElm.requestFullscreen) {
                docElm.requestFullscreen();
            } else if ((docElm).webkitRequestFullScreen) {
                (docElm).webkitRequestFullScreen();
            }
            this.isFullScreen = true;
        } else {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if ((document).webkitCancelFullScreen) {
                (document).webkitCancelFullScreen();
            }
            this.isFullScreen = false;
        }
    }

	//ts
	switchFullScreen(): void {
    const docElm = document.documentElement;
    if (!this.isFullScreen) {
      if (docElm.requestFullscreen) {
        docElm.requestFullscreen();
      } else if ((docElm as any).webkitRequestFullScreen) {
        (docElm as any).webkitRequestFullScreen();
      }
      this.isFullScreen = true;
    } else {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if ((document as any).webkitCancelFullScreen) {
        (document as any).webkitCancelFullScreen();
      }
      this.isFullScreen = false;
    }
  }

方案二


    function exitFullScreen() {
        $('#data_screen').data('open', false);
        $('#data_screen .item_title').text('全屏')

    };

    function enterFullScreen() {
        initCont();
        $('#data_screen').data('open', true);
        $('#data_screen .item_title').text('退出')
    };

    // 全屏
    $('#data_screen .item_title').on('click', () => {
        if ($('#data_screen').data('open')) {
            // 退出全屏
            // exitFullScreen();
            var cfs = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || document.exitFullScreen;
            if (cfs) {
                cfs.call(document);
            } else if (typeof window.ActiveXObject !== "undefined") {
                var wscript = new ActiveXObject("WScript.Shell");
                if (wscript != null) {
                    wscript.SendKeys("{F11}");
                }
            }
        } else {
            // 进入全屏
            // enterFullScreen()
            var docEle = document.documentElement;
            var rfs = docEle.requestFullScreen || docEle.webkitRequestFullScreen || docEle.mozRequestFullScreen || docEle.msRequestFullScreen;
            if (rfs) {
                rfs.call(docEle);
            } else if (typeof window.ActiveXObject !== "undefined") {
                var wscript = new ActiveXObject("WScript.Shell");
                if (wscript != null) {
                    wscript.SendKeys("{F11}");
                }
            }
        }

    });

    //  监听全屏事件触发
    function fullscreenchange() {
        let isFullScreen = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
        if (isFullScreen) {
            //  进入全屏
            enterFullScreen()
        } else {
            // 退出全屏
            exitFullScreen();
        }
    }

    ['fullscreenchange', 'webkitfullscreenchange', 'mozfullscreenchange'].forEach((item, index) => {
        window.addEventListener(item, () => fullscreenchange());
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

本地是好的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值