我为我当前的项目实现了一些全屏功能,这是脚本:
jQuery(document).ready(function ($) {
$(".full-screen-btn").bind("click", function () {
launchIntoFullscreen(document.getElementById("main-container"));
showExitFullScreenButton();
});
document.addEventListener("fullscreenchange", checkIfFullScreen);
document.addEventListener("webkitfullscreenchange", checkIfFullScreen);
document.addEventListener("mozfullscreenchange", checkIfFullScreen);
document.addEventListener("MSFullscreenChange", checkIfFullScreen);
function showFullScreenButton() {
$(".exit-full-screen-btn").addClass("full-screen-btn");
$(".exit-full-screen-btn").unbind('click')
$(".full-screen-btn").removeClass("exit-full-screen-btn");
$(".full-screen-btn").attr("value", "Full Screen");
$(".full-screen-btn").bind("click", function () {
launchIntoFullscreen(document.getElementById("main-container"));
showExitFullScreenButton();
});
}
function showExitFullScreenButton() {
$(".full-screen-btn").addClass("exit-full-screen-btn");
$(".full-screen-btn").unbind('click')
$(".exit-full-screen-btn").removeClass("full-screen-btn");
$(".exit-full-screen-btn").attr("value", "Exit");
$(".exit-full-screen-btn").bind("click", function () {
exitFullscreen();
showFullScreenButton();
});
}
function checkIfFullScreen() {
if ($(".full-screen-btn").length) {
showFullScreenButton();
} else {
showExitFullScreenButton();
}
}
// Find the right method, call on correct element
function launchIntoFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
// Close fullscreen
function exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
});
使用此脚本一切正常,除非用户使用浏览器功能退出全屏(如键盘中的 f11 和 ESC 按钮),这不会将我的退出按钮再次更新为全屏按钮(我已经看到了完整的Youtube 中的屏幕和最小化按钮,即使用户使用浏览器功能退出全屏,最小化按钮也会更新)。你们能帮帮我吗?谢谢。
这是正在被替换的 html 输入按钮:
<input type="button" value="Full Screen" class="full-screen-btn"/>