今天就跟大家聊聊有关使用JavaScript怎么实现窗口全屏显示,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<script src="js/jquery-1.11.3.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
/* 窗口全屏样式 */
html:-moz-full-screen {
background: grey;
}
html:-webkit-full-screen {
background: grey;
}
html:fullscreen {
background: grey;
}
div {
width: 100px;
height: 100px;
background: blue;
}
img {
width: 80px;
height: 80px;
}
</style>
</head>
<body>
<button class="win-fullscreen">窗口全屏</button>
<button class="div-fullscreen">div全屏</button>
<button class="img-fullscreen">img全屏</button>
<button class="exit-fullscreen">退出全屏</button>
<div class="div">
<img class="img" src="images/a.png">
</div>
</body>
<script>
/* 调用示例 */
// 窗口全屏
$('.win-fullscreen').on('click', function() {
requestFullScreen(document.documentElement);
});
// div全屏
$('.div-fullscreen').on('click', function() {
requestFullScreen($('.div')[0]);
});
// img全屏
$('.img-fullscreen').on('click', function() {
requestFullScreen($('.img')[0]);
});
// 退出全屏
$('.exit-fullscreen').on('click', function() {
exitFull();
});
// 窗口状态改变事件
fullscreenchange(document, function(isFull) {
console.log(isFull);
});
/* 封装 */
// 窗口状态改变
function fullscreenchange(el, callback) {
el.addEventListener("fullscreenchange", function () {
callback && callback(document.fullscreen);
});
el.addEventListener("webkitfullscreenchange", function () {
callback && callback(document.webkitIsFullScreen);
});
el.addEventListener("mozfullscreenchange", function () {
callback && callback(document.mozFullScreen);
});
el.addEventListener("msfullscreenchange", function () {
callback && callback(document.msFullscreenElement);
});
}
// 全屏
function requestFullScreen(element) {
var requestMethod = element.requestFullScreen || //W3C
element.webkitRequestFullScreen || //Chrome等
element.mozRequestFullScreen || //FireFox
element.msRequestFullScreen; //IE11
if (requestMethod) {
requestMethod.call(element);
}else if (typeof window.ActiveXObject !== "undefined") {//for Internet Explorer
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
}
//退出全屏
function exitFull() {
var exitMethod = document.exitFullscreen || //W3C
document.mozCancelFullScreen || //Chrome等
document.webkitExitFullscreen || //FireFox
document.webkitExitFullscreen; //IE11
if (exitMethod) {
exitMethod.call(document);
}else if (typeof window.ActiveXObject !== "undefined") {//for Internet Explorer
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
}
</script>
</html>
看完上述内容,你们对使用JavaScript怎么实现窗口全屏显示有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。