如何使用Javascript / JQuery代码进入全屏模式?目标是进入全屏模式,就像在浏览器中按F11,但是以编程方式。
解决方法
您可以使用没有jQuery的香草JavaScript激活全屏模式。
<!DOCTYPE html> <html> <head> <title>Full Screen Test</title> </head> <body id="body"> <h1>test</h1> <script> var elem = document.getElementById("body"); elem.onclick = function() { req = elem.requestFullScreen || elem.webkitRequestFullScreen || elem.mozRequestFullScreen; req.call(elem); } </script> </body> </html>
有一点很重要,您只能在用户执行操作(例如点击)时请求全屏模式。没有用户action[1],您无法请求全屏模式(例如在页面加载时)。
这是一个跨浏览器功能切换全屏模式(as obtained from the MDN):
function toggleFullScreen() { if (!document.fullscreenElement && // alternative standard method !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) { // current working methods if (document.documentElement.requestFullscreen) { document.documentElement.requestFullscreen(); } else if (document.documentElement.msRequestFullscreen) { document.documentElement.msRequestFullscreen(); } else if (document.documentElement.mozRequestFullScreen) { document.documentElement.mozRequestFullScreen(); } else if (document.documentElement.webkitRequestFullscreen) { document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); } } else { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } } }
有关更多信息,请查看MDN page on full screen APIs。
原文地址:https://www.jb51.cc/jquery/182232.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。