HTML5全屏浏览器兼容方案

摘要:
divide=“示例”>(函数(){document.documentElement.webkitRequestFullscreen();divi=“example”>//t.zoukankan.com/html5.png“>requestFullscreen<exitFullscreen<

最近一个项目有页面全屏的的需求,搜索了下有HTML5的全屏API可用,不过各浏览器的支持不一样。

标准webkitFirefoxIE
Element.requestFullscreen()webkitRequestFullscreenmozRequestFullScreenmsRequestFullscreen
Document.exitFullscreen()webkitExitFullscreenmozCancelFullScreenmsExitFullscreen
Document.fullscreenElementwebkitFullscreenElementmozFullScreenElementmsFullscreenElement
Document.fullscreenEnabledwebkitFullscreenEnabledmozFullScreenEnabledmsFullscreenEnabled
Document.fullscreenchangewebkitfullscreenchangemozfullscreenchangeMSFullscreenChange
Document.fullscreenerrorwebkitfullscreenerrormozfullscreenerrorMSFullscreenError

MDN Fullscreen API: The Fullscreen API provides an easy way for web content to be presented using the user's entire screen. The API lets you easily direct the browser to make an element and its children, if any, occupy the fullscreen, eliminating all browser user interface and other applications from the screen for the duration.

全屏接口提供了简单的方式通过用户整个屏幕展示浏览器的内容。这个接口让我们很轻松的引导浏览器使一个元素和它的子元素占据整个屏幕,并且从屏幕上消除所有浏览器用户界面和其它应用程序。

一、接口使用(以谷歌浏览器为例)

1.requestFullscreen()

全屏请求方法,使用方法:Element.requestFullscreen()

<div id="example">
    <img src="http://t.zoukankan.com/html5.png">
    <button type="button" id="requestFullscreen">requestFullscreen</button>
</div>

<script>
// 全屏
document.getElementById('requestFullscreen').addEventListener('click', () => {
    document.querySelector('img').webkitRequestFullscreen();
});
</script>

触发事件后会有按ESC即可退出全屏模式的文字提示。

注意

1.在< iframe >框架中使用全屏需要加allowfullscreen属性。 2.全屏请求只能通过用户操作触发,否则会出现Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture.这样的警告,解决办法是将此方法绑定到某个用户操作事件上,例如点击事件click

(function () {
    document.documentElement.webkitRequestFullscreen();
})();

2.exitFullscreen()

退出全屏模式的方法,使用方法:document.exitFullscreen(),除了requestFullscreen()其它方法和属性都是基于document的。

<div id="example">
    <img src="http://t.zoukankan.com/html5.png">
    <button type="button" id="requestFullscreen">requestFullscreen</button>
    <button type="button" id="exitFullscreen">exitFullscreen</button>
</div>

<script>
// 退出全屏
document.getElementById('exitFullscreen').addEventListener('click', () => {
    document.webkitExitFullscreen();
});
</script>

触发后退出全屏恢复页面原来的样子,也可以按ESC退出;另外F11也可以使页面全屏显示和退出,但这应该属于浏览器的功能,不在HTML5 API的范畴之内。

3.fullscreenElement

若是全屏模式下,显示全屏的元素,若不是,返回null

<div id="example">
    <img src="http://t.zoukankan.com/html5.png">
    <button type="button" id="requestFullscreen">requestFullscreen</button>
    <button type="button" id="exitFullscreen">exitFullscreen</button>
    <button type="button" id="fullscreenElement">fullscreenElement</button>
</div>

<script>
// 显示全屏元素
document.getElementById('fullscreenElement').addEventListener('click', () => {
    console.log(document.webkitFullscreenElement); // <div id=...></div> 或 null
});
</script>

4.fullscreenEnabled

返回一个布尔值true/false,判断是否可用全屏模式。

<div id="example">
    <img src="http://t.zoukankan.com/html5.png">
    <button type="button" id="fullscreenEnabled">fullscreenEnabled</button>
</div>

<script>
// 全屏是否可用
document.getElementById('fullscreenEnabled').addEventListener('click', () => {
    console.log(document.webkitFullscreenEnabled); // true
});
</script>

二、浏览器兼容

由于各主流浏览器调用全屏接口的方法不一致,所以调用之前需要判断一下当前浏览器适用的方法。

我简单的做了下请求全屏退出全屏的适配。

const MAZEY_FULL_SCREEN = function () {
    let prefixArr = ['', 'webkit', 'moz', 'ms'], // 浏览器前缀
        isRightRequest, // 是否找到适配的方法
        isRightExit,
        requestMethod, // 全屏方法
        exitMethod, // 退出全屏方法
        lowerFirst = function (str) {
            return str.slice(0, 1).toLowerCase() + str.slice(1);
        },
        requestSuffixArr = ['RequestFullscreen', 'RequestFullScreen'], // 后缀
        exitSuffixArr = ['ExitFullscreen', 'CancelFullScreen'],
        searchRightMethod = function (prefix, suffixArr, documentParent) {
            let methodArr = suffixArr.map((suffix) => {
                return prefix + suffix;
            }),
            method,
            isRight;
            methodArr.forEach((wholePrefix) => {
                if (isRight) return;
                if (prefix.length === 0) {
                    wholePrefix = lowerFirst(wholePrefix)
                }
                if (wholePrefix in documentParent) {
                    method = wholePrefix;
                    isRight = true;
                    // console.log(method);
                }
            });
            return method;
        };
    prefixArr.forEach((prefix) => {
        if (isRightRequest && isRightExit) return;
        // 查找请求
        requestMethod = searchRightMethod(prefix, requestSuffixArr, document.documentElement);
        isRightRequest = Boolean(requestMethod);
        // 查找退出
        exitMethod = searchRightMethod(prefix, exitSuffixArr, document);
        isRightExit = Boolean(exitMethod);
    });
    this.request = function (element) {
        let domEle = document.querySelector(element) || document.documentElement;
        domEle[requestMethod]();
    };
    this.exit = function () {
        document[exitMethod]();
    };
};

let fullscreen = new MAZEY_FULL_SCREEN();

使用示例:

<h1 id="h1">html5 - 全屏</h1>
<button id="request">请求</button>
<button id="exit">退出</button>
<script src="http://t.zoukankan.com/mazey-full-screen.js"></script>

<script>
// 请求全屏
document.getElementById('request').addEventListener('click', () => {
    fullscreen.request();
});
// 退出全屏
document.getElementById('exit').addEventListener('click', () => {
    fullscreen.exit();
});
</script>

示例代码:GitHub

HTML5全屏浏览器兼容方案

免责声明:文章转载自《HTML5全屏浏览器兼容方案》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ES9新内容概括Python基础知识下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

easyui datagrid设置fit: true后,页面显示不全的情况

跟工具栏有关 <div id="tb"> <div style="float:left;"> <a href="http://t.zoukankan.com/cuizhf-p-5240229.html#" class="easyui-linkbutto...

MyEclipse的多模块Maven web(ssm框架整合)

  Maven的多模块可以让项目结构更明确,提高功能的内聚,降低项目的耦合度,真正的体现出分层这一概念。   我们在操作中,要明白为什么这样做,要了解到更深的层次,这样,我们就不限于个别软件了。   话不多说,直入主题:   如果对Maven还不够熟悉,请看该博客:Maven基础   整个项目做完之后的结构是这样的:         在开始之前我放出这两张...

react中使用antd-mobile的dreawer抽屉组件从踩坑到自己造轮子

  1. 如果在使用过程中报这个错,意思是Drawer组件必须有一个子组件children,但现在还未定义       用官方例子解释就是<Drawer>这里面必须要有内容,如下图红色框部分内容</Drawer>    而这里面的内容一般为自定义代码      2. 如果你想让弹出的侧边导航栏在页面右侧       不要在state...

上传图片,预览并保存成blob类型 和 base64

场景: 获取到一个file类型的图片,如果直接在html中预览?这里就是利用html5的新特性,将图片转换为Base64的形式显示出来。有两种方法: 方法一:利用URL.createObjectURL() <!DOCTYPE html><html>  <head>     <title>base</ti...

C语言集锦(三)Direct3D和GDI+的例子

0.前言   有些时候你可能想了解,如何用纯C语言来写Direct3D和GDI+的Demo。注意,下面的Direct3D例子不适用于TCC编译器,GDI+的例子是可以的。 1.Direct3D C语言的例子   几乎所有的D3D例子都是用COM和C++写的。C语言可以用D3D吗,StackOverflow上给出了答案:directx-programming...

Sublime编辑VUE实现代码高亮

❤记在前面 今天没啥特殊的,送自己一句话吧:  欲戴王冠,必承其重         ——《继承者们》 一、下载packages资源 传送网址:https://pan.baidu.com/s/11UkY_Yqr3OYTP6xtnaeLYA ♥ 下载完之后,将解压文件放入Sublime Text安装文件夹下的Data->packages ♥ 打开su...