标签搜索

目 录CONTENT

文章目录

JS实现全屏和退出全屏

沙漠渔
2023-04-04 00:13:43 / 0 评论 / 0 点赞 / 529 阅读 / 895 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-04-04,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

界面按钮

<button onclick="toggleFullScreen(event)">点我全屏</button>

相应的js代码:

function FullScreen(el) {
    var isFullscreen = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
    if (!isFullscreen) { //进入全屏,多重短路表达式
        (el.requestFullscreen && el.requestFullscreen()) ||
        (el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
        (el.webkitRequestFullscreen && el.webkitRequestFullscreen()) || (el.msRequestFullscreen && el.msRequestFullscreen());
    } else { //退出全屏,三目运算符
        document.exitFullscreen ? document.exitFullscreen() :
            document.mozCancelFullScreen ? document.mozCancelFullScreen() :
            document.webkitExitFullscreen ? document.webkitExitFullscreen() : '';
    }
}

function toggleFullScreen(e) {
    var el = e.srcElement || e.target; //target兼容Firefox
    el.innerHTML == '点我全屏' ? el.innerHTML = '退出全屏' : el.innerHTML = '点我全屏';
    FullScreen(el);
}
0
广告 广告

评论区