0

我为我当前的项目实现了一些全屏功能,这是脚本:

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"/>
4

2 回答 2

0

我通过使用keyup Eventin javascript 找到了答案:

function checkIfFullScreen() {
    if ($(".full-screen-btn").length) {
        showFullScreenButton();
    } else {

        showExitFullScreenButton();
    }

    // check if f11 or ESC button was pressed
    $(document).keyup(function (e) {
        if (e.which == 122 || e.which == 27) {
            e.preventDefault();
            showFullScreenButton();
        }
    });
}
于 2015-09-15T05:02:30.497 回答
0

不确定,但我认为你应该这样做:

function exitFullscreen() {

    showFullScreenButton(); // THIS IS MY EDIT

    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    }
}
于 2015-09-15T02:59:00.923 回答