JavaScript实现全屏与退出全屏

function requestFullscreen(element) {

if(element.requstFullscreen) {//w3c
element.requstFullscreen();
} else if(element.mozRequestFullScreen) {//firefox
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {//chrome
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {//ie11
element.msRequestFullscreen();
}
}
// 启动全屏
requestFullscreen(document.documentElement); // 整个网页
requestFullScreen(document.getElementById("videoElement")); // 某个页面元素

 退出全屏:function exitFullscreen() {

//W3C
if (document.exitFullscreen)
{
  document.exitFullscreen();
}
//FireFox
else if (document.mozCancelFullScreen)
{
  document.mozCancelFullScreen();
}
//Chrome等
else if (document.webkitCancelFullScreen)
{
  document.webkitCancelFullScreen();
}
//IE11
else if (document.msExitFullscreen)
{
  document.msExitFullscreen();
}
}

使用全屏在chrome出现Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture.

非用户行为无法进行全屏

posted @ 2020-11-26 11:09  天明宝  阅读(527)  评论(0编辑  收藏  举报