最新消息:文章中包含代码时,请遵守代码高亮规范!

js+css实现全屏【转载】

HTML+CSS Cindy 66浏览 0评论

在html5中,全屏方法可以适用于很多html 元素,不仅仅是video。以下是实现全屏具体代码:
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%;
}


//反射调用
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);

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/22726.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Cindy的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!