• dev for pro

浏览器全屏实现方式

1.利用h5的requestFullScreen

2.摁F11实现全屏效果

requestFullscreen全屏具体实现

1.进入全屏
function full(ele) {
      if (ele.requestFullscreen) {
          ele.requestFullscreen();
      } else if (ele.mozRequestFullScreen) {
          ele.mozRequestFullScreen();
      } else if (ele.webkitRequestFullscreen) {
          ele.webkitRequestFullscreen();
      } else if (ele.msRequestFullscreen) {
          ele.msRequestFullscreen();
      }
}

ele:要全屏的元素,可以是document.body也可以是某一个div 思路: 1.判断该浏览器是否具有requestFullscreen方法 2.有,则直接执行ele.requestFullscreen();没有则做浏览器兼容判断。

2.exitFullScreen退出全屏
function exitFullscreen() {
    if(document.exitFullScreen) {
        document.exitFullScreen();
    } else if(document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if(document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    } else if(document.msExitFullscreen) {
        document.msExitFullscreen();
    }
}

退出全屏直接使用document调用exitFullscreen方法即可。

3.获取当前全屏的节点
function getFullscreenElement() {
    return (        
        document.fullscreenElement ||
        document.mozFullScreenElement ||
        document.msFullScreenElement ||
        document.webkitFullscreenElement||null
    );
}
document.fullscreenElement()`:获取当前全屏的元素。
 假设id为div1的Element当前为全屏状态则 `document.querySelector("#div1")
 document.fullscreenElement
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退出全屏

<style>
    html,
    body {
        position: relative;
        display: grid;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        margin: 0;
    }

    .main {
        width: 50vw;
        padding: 2em;
        text-align: center;
        transition: background-color 100ms ease;
    }

    .main:fullscreen {
        background-color: DarkRed;
        color: white;
    }
</style>

<body>
    <section class="main">
        <h1>元素在全屏显示模式下的CSS样式</h1>
        <p>css伪类:fullscreen应用于当前处于全屏显示模式的元素。</p>
        <button id="toggle-pattern">全屏模式</button>
    </section>
</body>

<script>


    const targetElement = document.querySelector('.main');
    const togglePatternButton = document.querySelector('#toggle-pattern');
    togglePatternButton.addEventListener('click', _ => {
        if (!document.fullscreenElement) {
            targetElement?.requestFullscreen();
            togglePatternButton.textContent = '退出全屏';
        } else {
            document.exitFullscreen();
            togglePatternButton.textContent = '全屏模式';
        }
    });

    function full(ele) {
        if (ele.requestFullscreen) {
            ele.requestFullscreen();
        } else if (ele.mozRequestFullScreen) {
            ele.mozRequestFullScreen();
        } else if (ele.webkitRequestFullscreen) {
            ele.webkitRequestFullscreen();
        } else if (ele.msRequestFullscreen) {
            ele.msRequestFullscreen();
        }
    }



    function exitFullscreen() {
        if (document.exitFullScreen) {
            document.exitFullScreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    }


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

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

    function isFullscreenEnabled() {
        return (
            document.fullscreenEnabled ||
            document.mozFullScreenEnabled ||
            document.webkitFullscreenEnabled ||
            document.msFullscreenEnabled
        );
    }
</script>