JavaScript: FullScreen

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="apple-moible-web-app-capable" content="yes">
<meta name="apple-moible-web-app-status-bar-style" content="black">	
<title>FullScreen</title>
	<meta content="font,孤僻字,如:叮𪠽,涂聚文,Geovin Du,塗聚文,geovindu,捷为工作室" name="keywords">
<meta content="font,孤僻字,涂聚文,Geovin Du,塗聚文,geovindu,捷为工作室" name="description">
	<style>
    .box{
        width: 200px;
        height: 200px;
        background-color: red;
    }
	</style>
<script>
	// 进入全屏  https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullScreen
	//https://www.w3schools.com/jsref/met_element_requestfullscreen.asp
    const enterFullScreen = (el) => {
      if (el.requestFullscreen) {
        el.requestFullscreen();
      } else if (el.mozRequestFullScreen) {
        el.mozRequestFullScreen();
      } else if (el.webkitRequestFullscreen) {
        el.webkitRequestFullscreen();
      } else if (el.msRequestFullscreen) {
        el.msRequestFullscreen();
      }
    }
	
	// 退出全屏
    const exitFullscreen = () => {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      }
    }
	
	// 当前处于全屏的元素
    const fullscreenElement = () => {
      return document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || null;
    }
	
	 // 是否处于全屏
    const isFullscreen = () => {
      return !!fullscreenElement()
    }
	
	
	 // 切换
    const toggleFullScreen = () => {
        isFullscreen()?
        exitFullscreen():
        enterFullScreen(el);
    }
	
	</script>
</head>

<body>
<div class="box" id="box"></div>

<script>
    const el = document.getElementById('box');
    el.onclick = toggleFullScreen;
</script>
</body>
</html>

  

posted @ 2024-04-29 09:15  ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报