[HTML]JS全屏代码
video全屏参考:https://www.thecssninja.com/javascript/fullscreen
注:IE不可用
<!doctype html> <html> <head> <meta charset="utf-8"> <title>JS全屏</title> <style> html:-moz-full-screen {background: red} html:-webkit-full-screen {background: red} html:fullscreen {background: red} </style> </head> <body> <a href="#" onClick="fullscreen()">fullscreen</a> | <a href="#" onClick="exitFullscreen()">exitFullscreen</a> <p id="state">no</p> <script> // 全屏 function fullscreen() { var docElm = document.documentElement; if (docElm.requestFullscreen) { docElm.requestFullscreen(); } else if (docElm.mozRequestFullScreen) { docElm.mozRequestFullScreen(); } else if (docElm.webkitRequestFullScreen) { docElm.webkitRequestFullScreen(); } else if (docElm.msRequestFullscreen) { docElm.msRequestFullscreen(); } } // 退出全屏 function exitFullscreen() { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } } // 监听是否全屏 window.onload = function() { var elem = document.getElementById('state'); document.addEventListener('fullscreenchange', function() { elem.innerText = document.fullscreen ? 'yes': 'no'; }, false); document.addEventListener('mozfullscreenchange', function() { elem.innerText = document.mozFullScreen ? 'yes': 'no'; }, false); document.addEventListener('webkitfullscreenchange', function() { elem.innerText = document.webkitIsFullScreen ? 'yes': 'no'; }, false); document.addEventListener('msfullscreenchange', function() { elem.innerText = document.msFullscreenElement ? 'yes': 'no'; }, false); } </script> </body> </html>
双击进入/退出全屏
function getFullscreenElement() {
return document.fullscreenElement //standard property
|| document.webkitFullscreenElement //safari/opera support
|| document.mozFullscreenElement //firefox support
|| document.msFullscreenElement //ie/edge support
}
function toggleFullscreen() {
if (getFullscreenElement()) {
document.exitFullscreen()
} else {
document.documentElement.requestFullscreen().catch(console.log)
}
}
document.addEventListener('dblclick', () => {
toggleFullscreen()
})
个性签名:做要做好,做到不三不四不如不做。