1、实现全屏化
var docElm = document.documentElement;
//W3C
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
}
//FireFox
else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
}
//Chrome等
else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
}
//IE11
else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
}
2、退出全屏化
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
}
else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
退出全屏化的问题来了,如果想用 ESC键 在退出全屏化的时候监听这个事件,经测试在IE中是可以监听到,firefox和chrome 中都无法监听到。
为了达到监听 ESC的效果,解决方案如下:
window.onresize = function(){
if(!checkFull()){
//要执行的动作
}
}
function checkFull(){
var isFull = document.fullscreenEnabled || window.fullScreen || document.webkitIsFullScreen || document.msFullscreenEnabled;
//to fix : false || undefined == undefined
if(isFull === undefined) isFull = false;
return isFull;
}
退出全屏化之后监控 window的resize事件,resize触发之后最好取消掉这个监听。