HTML5 video player full screen (fullScreen) method example, html5fullscreen
This article mainly introduces the full screen (fullScreen) method example of HTML5 video player. This article provides a complete code example. For more information, see
First, this title is misleading, but it is mainly because video is used more often.
In html5, the full screen method can be applied to many html elements, not just video
The Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> full screen </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"> full screen </button>
</Div>
<Script type = "text/javascript">
// Used for reflection
Var invokeFieldOrMethod = function (element, method)
{
Var usablePrefixMethod;
["Webkit", "moz", "ms", "o", ""]. forEach (function (prefix ){
If (usablePrefixMethod) return;
If (prefix = ""){
// No prefix. The first letter of the method is lowercase.
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;
};
// Enter full screen
Function launchFullscreen (element)
{
// This method cannot be used in other tasks. Otherwise, Firefox cannot be used in full screen mode.
If (element. requestFullscreen ){
Element. requestFullscreen ();
} Else if (element. inclurequestfullscreen ){
Element. Required requestfullscreen ();
} Else if (element. msRequestFullscreen ){
Element. msRequestFullscreen ();
} Else if (element. oRequestFullscreen ){
Element. oRequestFullscreen ();
}
Else if (element. webkitRequestFullscreen)
{
Element. webkitRequestFullScreen ();
} Else {
Var docHtml = document.doc umentElement;
Var docBody = document. body;
Var videobox = document. getElementById ('videobox ');
Var cssText = 'width: 100%; height: 100%; overflow: hidden ;';
DocHtml.style.css Text = cssText;
DocBody.style.css Text = cssText;
Videobox.style.css Text = cssText + ';' + 'margin: 0px; padding: 0px ;';
Document. IsFullScreen = true;
}
}
// Exit full screen
Function exitFullscreen ()
{
If (document. exitFullscreen ){
Document. exitFullscreen ();
} Else if (document. msExitFullscreen ){
Document. msExitFullscreen ();
} Else if (document. canccancelfullscreen ){
Document. Define cancelfullscreen ();
} Else if (document. oRequestFullscreen ){
Document. oCancelFullScreen ();
} Else if (document. webkitExitFullscreen ){
Document. webkitExitFullscreen ();
} Else {
Var docHtml = document.doc umentElement;
Var docBody = document. body;
Var videobox = document. getElementById ('videobox ');
DocHtml.style.css Text = "";
DocBody.style.css Text = "";
Videobox.style.css Text = "";
Document. IsFullScreen = false;
}
}
Document. getElementById ('fullscreenbtn '). addEventListener ('click', function (){
LaunchFullscreen (document. getElementById ('video '));
Window. setTimeout (function exit (){
// Check whether the browser is in full screen mode.
If (invokeFieldOrMethod (document, 'fullscreen ') | invokeFieldOrMethod (document, 'isfullscreen') | document. IsFullScreen)
{
ExitFullscreen ();
}
}, 5*1000 );
}, False );
</Script>
</Body>
</Html>