首頁 >web前端 >H5教程 >HTML5 video播放器全螢幕(fullScreen)實作的方法

HTML5 video播放器全螢幕(fullScreen)實作的方法

不言
不言原創
2018-06-11 17:17:4429184瀏覽

這篇文章主要介紹了HTML5 video播放器全螢幕(fullScreen)方法實例,本文直接給出一個完整程式碼實例,需要的朋友可以參考下

首先來說,這個標題具有誤導性,但這樣設定改標題也是主要因為video使用的比較多

在html5中,全螢幕方法可以適用於很多html 元素,不只是video

<!doctype  html>
<html>
<head>
<meta charset="utf-8" />
<title>全屏问题</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="imagetoolbar" content="no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<style type="text/css">
*{
   padding: 0px;
   margin: 0px;
}

body p.videobox{
   width: 400px;
   height: 320px;
   margin: 100px auto;
   background-color:#000;
}

body p.videobox video.video
{
width: 100%;
   height: 100%;
}

:-webkit-full-screen {

}

:-moz-full-screen {

}

:-ms-fullscreen {

}

:-o-fullscreen {

}

:full-screen { 

}

:fullscreen {

}

:-webkit-full-screen video {
 width: 100%;
 height: 100%;
}
:-moz-full-screen video{
   width: 100%;
   height: 100%;
}
   </style>
</head>
<body>


<p id="videobox">

   <video controls="controls" preload="preload" id="video" poster="poster.jpg">
     <source src="./movie.ogg" type="video/ogg" />
     <source src="./movie.mp4" type="video/mp4" />
     <source src="./movie.webm" type="video/webm" />
     <object data="./movie.mp4" width="100%" height="100%">
       <embed width="100%" height="100%" src="./movie.swf" />
     </object>

   </video>
   <button id="fullScreenBtn">全屏</button>
</p>


<script type="text/javascript">

//反射調用
var invokeFieldOrMethod = function(element, method) 
{
  var usablePrefixMethod;
  ["webkit", "moz", "ms", "o", ""].forEach(function(prefix) {
      if (usablePrefixMethod) return;
      if (prefix === "") {
          // 无前缀,方法首字母小写
          method = method.slice(0,1).toLowerCase() + method.slice(1);   
      }
      var typePrefixMethod = typeof element[prefix + method];
      if (typePrefixMethod + "" !== "undefined") {
          if (typePrefixMethod === "function") {
              usablePrefixMethod = element[prefix + method]();
          } else {
              usablePrefixMethod = element[prefix + method];
          }
      }
  });

      return usablePrefixMethod;
};

//進入全屏
function launchFullscreen(element) 
  {
   //此方法不可以在異步任務中執行,否則火狐無法全屏
    if(element.requestFullscreen) {
      element.requestFullscreen();
    } else if(element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if(element.msRequestFullscreen){ 
      element.msRequestFullscreen();  
    } else if(element.oRequestFullscreen){
       element.oRequestFullscreen();
   }
   else if(element.webkitRequestFullscreen)
    {
      element.webkitRequestFullScreen();
    }else{

       var docHtml  = document.documentElement;
       var docBody  = document.body;
       var videobox  = document.getElementById(&#39;videobox&#39;);
       var  cssText = &#39;width:100%;height:100%;overflow:hidden;&#39;;
       docHtml.style.cssText = cssText;
       docBody.style.cssText = cssText;
       videobox.style.cssText = cssText+&#39;;&#39;+&#39;margin:0px;padding:0px;&#39;;
       document.IsFullScreen = true;

     }
  }
//退出全屏
  function exitFullscreen()
  {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if(document.oRequestFullscreen){
           document.oCancelFullScreen();
       }else if (document.webkitExitFullscreen){
        document.webkitExitFullscreen();
      }else{
       var docHtml  = document.documentElement;
       var docBody  = document.body;
       var videobox  = document.getElementById(&#39;videobox&#39;);
       docHtml.style.cssText = "";
       docBody.style.cssText = "";
       videobox.style.cssText = "";
       document.IsFullScreen = false;
   }
 }
document.getElementById(&#39;fullScreenBtn&#39;).addEventListener(&#39;click&#39;,function(){
   launchFullscreen(document.getElementById(&#39;video&#39;)); 
   window.setTimeout(function exit(){
//檢查瀏覽器是否處於全屏
if(invokeFieldOrMethod(document,&#39;FullScreen&#39;) || invokeFieldOrMethod(document,&#39;IsFullScreen&#39;) || document.IsFullScreen)
{
exitFullscreen();
}
  },5*1000);
},false);

</script>
</body>

</html>

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

HTML5 video標籤的播放控制

關於H5新屬性audio音訊和video視訊的控制解析

以上是HTML5 video播放器全螢幕(fullScreen)實作的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn