HTML页面的全屏显示-Fullscreen API

HTML页面的全屏显示-Fullscreen API使用 FullscreenAP 处理页面中的全屏问题

使用 Fullscreen api 处理页面全屏

HTML 页面的全屏显示

使用 Element.requestFullscreen() 可以使元素进入全屏,该方法是异步方法,返回一个 Promise 对象

整个页面全屏显示

比如我们需要让整个网页全屏显示,只需要用 html 元素调用 requestFullscreen 方法即可。

示例代码:

<html> <body> <div> 全屏显示案例 <button id="full-screen-btn">进入全屏 
     button>  
      div>  
       body> <script> const html = document.querySelector('html'); const fullScreenBtn = document.getElementById('full-screen-btn'); fullScreenBtn.onclick = () => { 
        html.requestFullscreen() .then(() => { 
        console.log('进入全屏成功')}) .catch(() => { 
        console.log('进入全屏失败')}) }  
        script> <html> 

参数

requestFullScreen 接收一个参数 options(可选), options 是一个对象, 但是只有一个字段 navigationUI, 用于控制是否在元素处于全屏模式时显示导航条.
可选值为 auto, hide, show, 默认值为 auto.

当元素不在文档内时, 调用requestFullScreen回失败

<html> <body> <div> 全屏显示案例 <button id="full-screen-btn">进入全屏 
     button>  
      div>  
       body> <script> const html = document.querySelector('html'); const fullScreenBtn = document.getElementById('full-screen-btn'); fullScreenBtn.onclick = () => { 
        html.requestFullscreen() .then(() => { 
        console.log('进入全屏成功')}) .catch(() => { 
        console.log('进入全屏失败')}) } // 不在文档内的内容全屏 const el = document.createElement('div'); el.requestFullscreen() .then(() => { 
        console.log('全屏成功'); }) .catch(() => { 
        console.log('全屏失败'); });  
        script> <html> 

部分内容进入全屏状态

是部分内容进入全屏和页面全屏是一样的, 只是调用 requestFullScreen 方法的 dom 节点改为相应元素即可.

示例代码:

<html> <header> <style> div { 
     padding: 20px; } #section-full-container { 
     background-color: #409EFF; color: #fff; height: 200px; }  
     style>  
      header> <body> <div> 全屏显示案例 <button id="full-screen-btn">进入全屏 
       button>  
        div> <div id="section-full-container"> 部分全屏 <button id="section-full-screen-btn">进入全屏 
         button>  
          div>  
           body> <script> const html = document.querySelector('html'); const fullScreenBtn = document.getElementById('full-screen-btn'); fullScreenBtn.onclick = () => { 
            html.requestFullscreen() .then(() => { 
            console.log('进入全屏成功'); }) .catch(() => { 
            console.log('进入全屏失败'); }) } // 部分内容全屏 const sectionFullContainer = document.getElementById('section-full-container'); const sectionFullBtn = document.getElementById('section-full-screen-btn'); sectionFullBtn.onclick = () => { 
            sectionFullContainer.requestFullscreen() .then(() => { 
            console.log('全屏成功'); }) .catch(() => { 
            console.log('全屏失败'); }); }  
            script>  
             html> 

退出全屏

退出全屏只需要调用 document 对象的 exitFullscreen. 调用这个方法会让文档回退到上一个调用Element.requestFullscreen()方法进入全屏模式之前的状态.
例如, 上面示例中, 先使整个页面进入全屏, 再点击部分全屏的按钮使 section-full-container 进入全屏. 那么整个时候调用 document.exitFullScreen() 时, 会返回整个页面全屏的状态, 需要再次调用 document.exitFullScreen() 才能完全退出全屏状态

示例代码

<html> <header> <style> div { 
     padding: 20px; } #section-full-container { 
     background-color: #409EFF; color: #fff; height: 200px; }  
     style>  
      header> <body> <div> 全屏显示案例 <button id="full-screen-btn">进入全屏 
       button> <button id="exit-full-screen-btn">退出全屏 
        button>  
         div> <div id="section-full-container"> 部分全屏 <button id="section-full-screen-btn">进入全屏 
          button> <button id="section-exit-full-screen-btn">退出全屏 
           button>  
            div>  
             body> <script> const html = document.querySelector('html'); const fullScreenBtn = document.getElementById('full-screen-btn'); fullScreenBtn.onclick = () => { 
              html.requestFullscreen() .then(() => { 
              console.log('进入全屏成功'); }) .catch(() => { 
              console.log('进入全屏失败'); }) } const sectionFullContainer = document.getElementById('section-full-container'); const sectionFullBtn = document.getElementById('section-full-screen-btn'); sectionFullBtn.onclick = () => { 
              sectionFullContainer.requestFullscreen() .then(() => { 
              console.log('全屏成功'); }) .catch(() => { 
              console.log('全屏失败'); }); } // 退出全屏 const exitFullScreenBtn = document.getElementById('exit-full-screen-btn'); const sectionExitFullScreenBtn = document.getElementById('section-exit-full-screen-btn'); exitFullScreenBtn.onclick = () => { 
              exitFullScreen(); } sectionExitFullScreenBtn.onclick = () => { 
              exitFullScreen(); }  
              script>  
               html> 

注意该方法是 document 对象的而不是对应元素的

事件与属性

当全屏状态改变时, 对应元素会触发 fullscreenchange 事件, 该事件会冒泡, 实际使用可以统一监听 document 对象的 fullscreenchange 事件.
document.fullScreenElement 属性可以访问当前正在全屏的元素节点. 没有页面全屏是返回 null.

示例代码

<html> <header> <style> div { 
     padding: 20px; } #section-full-container { 
     background-color: #409EFF; color: #fff; height: 200px; }  
     style>  
      header> <body> <div> 全屏显示案例 <button id="full-screen-btn">进入全屏 
       button> <button id="exit-full-screen-btn">退出全屏 
        button>  
         div> <div id="section-full-container"> 部分全屏 <button id="section-full-screen-btn">进入全屏 
          button> <button id="section-exit-full-screen-btn">退出全屏 
           button>  
            div>  
             body> <script> const html = document.querySelector('html'); const fullScreenBtn = document.getElementById('full-screen-btn'); fullScreenBtn.onclick = () => { 
              html.requestFullscreen() .then(() => { 
              console.log('进入全屏成功'); }) .catch(() => { 
              console.log('进入全屏失败'); }) } const sectionFullContainer = document.getElementById('section-full-container'); const sectionFullBtn = document.getElementById('section-full-screen-btn'); sectionFullBtn.onclick = () => { 
              sectionFullContainer.requestFullscreen() .then(() => { 
              console.log('全屏成功'); }) .catch(() => { 
              console.log('全屏失败'); }); } const exitFullScreenBtn = document.getElementById('exit-full-screen-btn'); const sectionExitFullScreenBtn = document.getElementById('section-exit-full-screen-btn'); exitFullScreenBtn.onclick = () => { 
              exitFullScreen(); } sectionExitFullScreenBtn.onclick = () => { 
              exitFullScreen(); } // 监听事件 document.onfullscreenchange = () => { 
              console.log('全屏状态变更'); // 访问当前全屏的节点 console.log('当前全屏节点为: ', document.fullscreenElement); }  
              script>  
               html> 

元素的全屏样式

使用伪类 :fullscreen, 可以给元素设置全屏时的属性, 当页面没有进入全屏是不生效, 全屏后才生效.
是当前元素属于全屏元素的时候才生效, 既当前元素为 document.fullScreenElement.
如果是父级或父级以上元素全屏, 该元素的 :fullscreen 样式并不会生效




注意: 该方法属于实验性方法, 有兼容性问题, 不建议实际项目中使用

示例代码

<html> <head> <style> #container { 
     background-color: #409EFF; color: #fff; height: 200; } /* 进入全屏是使元素变为绿色 */ #container:fullscreen { 
     background-color: #67C23A; }  
     style>  
      head> <body> <div id="container"> fullscreen 伪类案例 <button id="full-screen-btn">进入全屏 
       button>  
        div>  
         body> <script> const container = document.getElementById('container'); const fullScreenBtn = document.getElementById('full-screen-btn'); fullScreenBtn.onclick = () => { 
          container.requestFullscreen(); }  
          script> <html> 

解决兼容性问题

兼容性问题可以参考 MDN 相关说明, 实际使用中可以使用 fullscreen 包进行全屏显示处理.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/204397.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午8:23
下一篇 2026年3月19日 下午8:23


相关推荐

  • 方差及常见分布的方差计算与推导公式_超几何分布的期望和方差

    方差及常见分布的方差计算与推导公式_超几何分布的期望和方差1.介绍方差定义和性质2.离散型随机变量(01分布,二项分布,泊松分布,几何分布,超几何分布)和连续型随机变量(均匀分布,指数分布,正态分布)分布的方差计算以及推导过程,并汇总形成表格,方便查阅和记录

    2026年1月14日
    5
  • Emgucv环境配置[通俗易懂]

        Emgucv是在.NET平台下使用OpenCV视觉库的桥梁,在使用之前需要对系统进行配置,其配置和OpenCV的配置有点不同。1、EmguCV下载下载网站:http://www.emgu.com/wiki/index.php/Main_Page该网站上有EmguCV的所有资料,包括教程。下载好之后,直接安装到电脑上,安装位置可任意。本文所配置的是EmguCV3.0.0版本。2、新建一个VS…

    2022年4月14日
    89
  • AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(一)

    AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(一)随着IOS7由之前UI的拟物化设计变为如今的扁平化设计,也许扁平化的时代要来了,当然我们是不是该吐槽一下,苹果什么时候也开始跟风了,自GOOGLE和微软界面扁平化过后,苹果也加入了这一队伍。AvalonDock  AvalonDock是一个.NET库,用于在停靠模式布局(docking)中排列一系列WPF/WinForm控件。最新发布的版本原生支持MVVM框架、AeroSnap特效…

    2022年7月20日
    16
  • 从头开始学MySQL——-存储过程与存储函数(1)

    从头开始学MySQL——-存储过程与存储函数(1)10.1.1创建存储过程存储过程就是一条或者多条SQL语句的集合,可以视为批文件。它可以定义批量插入的语句,也可以定义一个接收不同条件的SQL。创建存储过程的语句为CREATEPROCEDURE,创建存储函数的语句为CREATEFUNCTION。调用存储过程的语句为CALL。调用存储函数的形式就像调用MyS……

    2022年8月22日
    12
  • 为什么要进行分销?

    为什么要进行分销?

    2021年6月17日
    97
  • CloseableHttpClient简单使用实例[通俗易懂]

    importjava.io.BufferedReader;importjava.io.IOException;importjava.nio.charset.Charset;importjava.security.KeyManagementException;importjava.security.KeyStoreException;importjava.security.NoSuchAlgorithmException;importjavax.net.ssl.SSLContext

    2022年4月10日
    201

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号