JS 全屏和退出全屏--requestFullScreen

·  阅读 103

浏览器全屏实现方式

1.利用h5的 requestFullScreen

2.F11实现全屏效果

requestFullscreen全屏

1.requestFullscreen 进入全屏

兼容判断

function openFullScreen(ele) {
    if (ele.requestFullscreen) {
        ele.requestFullscreen();
    } else if (ele.mozRequestFullScreen) {
        ele.mozRequestFullScreen();
    } else if (ele.webkitRequestFullscreen) {
        ele.webkitRequestFullscreen();
    } else if (ele.msRequestFullscreen) {
        ele.msRequestFullscreen();
    }
}
复制代码

2.exitFullScreen 退出全屏

function exitFullScreen(ele) {
    if(document.exitFullScreen) {
        document.exitFullScreen();
    } else if(document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if(document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    } else if(document.msExitFullscreen) {
        document.msExitFullscreen();
    }
}
复制代码

3.获取当前全屏节点

function getFullscreenElement() {
    return (
        document.fullscreenElement ||
        document.mozFullScreenElement ||
        document.msFullScreenElement ||
        document.webkitFullscreenElement||null
    );
}
复制代码

4.判断当前是否全屏

function isFullScreen() {
    return !! (
        document.fullscreen ||
        document.mozFullScreen ||
        document.webkitIsFullScreen ||
        document.webkitFullScreen ||
        document.msFullScreen
    );
}
复制代码

5.判断是否能切换全屏

function isFullscreenEnabled() {
    return (
        document.fullscreenEnabled ||
        document.mozFullScreenEnabled ||
        document.webkitFullscreenEnabled ||
        document.msFullscreenEnabled
    );
}
复制代码

注意事项:

1.document下没有requestFullscreen
2.requestFullscreen方法只能由用户触发,比如:在onload事件中不能触发
3.页面跳转需先退出全屏
4.进入全屏的元素,将脱离其父元素,所以可能导致之前某些css的失效
解决方案:使用 :full-screen伪类 为元素添加全屏时的样式(使用时为了兼容注意添加-webkit、-moz或-ms前缀)
5.一个元素A全屏后,其子元素要再全屏,需先让元素A退出全屏

requestFullscreen兼容性

image.png

作者:皮卡丘_小面包
链接:www.jianshu.com/p/54729c736…
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

分类:
前端
分类:
前端
收藏成功!
已添加到「」, 点击更改