這篇文章主要介紹了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 div.videobox{
width: 400px;
height: 320px;
margin: 100px auto;
background-color:#000;
}
body div.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>
<div 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>
</div>
<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('videobox');
var CSSText = 'width:100%;height:100%;overflow:hidden;';
docHtml.style.cssText = CSSText;
docBody.style.cssText = CSSText;
videobox.style.cssText = CSSText+';'+'margin:0px;padding:0px;';
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('videobox');
docHtml.style.CSSText = "";
docBody.style.CSSText = "";
videobox.style.CSSText = "";
document.IsFullScreen = false;
}
}
document.getElementById('fullScreenBtn').addEventListener('click',function(){
launchFullscreen(document.getElementById('video'));
window.setTimeout(function exit(){
//查器是否於全屏
if(invokeFieldOrMethod(document,'FullScreen') || invokeFIEldOrMethod(document,'IsFullScreen') || document.IsFullScreen)
{
exitFullscreen();
}
},5*1000);
},false);
</script>
</body>
</Html>