js 实现全屏

全屏是否可用:Document.fullscreenEnabled
开启全屏:Document.requestFullScreen() 该方法返回 promise
退出全屏:Document.exitFullscreen() 让当前元素退出全屏模式,会有一个全屏栈
获取元素全屏状态:Document.mozFullScreenElement
全屏模式变化事件: fullscreenchange
参考博客

全屏方法需要用户手动操作的事件触发,如点击等

fullScreen() {
  let el = this.$refs.vbox; // 获取需要全屏的 dom

  /*
  if (el.requestFullscreen) {
    el.requestFullscreen();
  } else if (el.webkitRequestFullScreen) {
    el.webkitRequestFullScreen();
  } else if (el.mozRequestFullScreen) {
    el.mozRequestFullScreen();
  } else if (el.msRequestFullscreen) {
    el.msRequestFullscreen();
  }
  */
  el.requestFullscreen||el.mozRequestFullScreen||el.webkitRequestFullscreen||el.msRequestFullScreen?
  el.requestFullscreen()||el.mozRequestFullScreen()|| el.webkitRequestFullscreen()||el.msRequestFullscreen():null;
  // 可以更新全屏的标志变量
}

这种方案虽然简单,但无法与其他组件内容配合,如抽屉等功能
此 api 在 iPhone 上不受支持

screenfull.js

简化写兼容性方法,浅封装
npm screenfull

useFullscreen

vueuse 提供的全屏功能

单页面应用中

可以有单独的全屏页面,全屏的同时因为是单页面可以兼顾其他组件。不会出现遮挡的情况
确定不能跳转时自动全屏


测试 demo

DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Documenttitle>
head>

<body>
	<div id="test" style="width: 200px; height: 100px; background-color: green;" onclick="fullScreen()">div>
	<script>
		function fullScreen() {
			el = document.getElementById('test')
			el.requestFullscreen || el.mozRequestFullScreen || el.webkitRequestFullscreen || el.msRequestFullScreen ?
				el.requestFullscreen() || el.mozRequestFullScreen() || el.webkitRequestFullscreen() || el.msRequestFullscreen() :
				null;
		}
	script>
body>

html>

你可能感兴趣的:(#,HTML,javascript,开发语言,ecmascript)