在浏览器中退出全屏的按钮不会更新

时间:2015-09-15 02:46:19

标签: javascript jquery html html5-fullscreen

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

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按钮),并且没有再次将我的退出按钮更新为全屏按钮,否则此脚本的一切正常。(I& #39;已经看到Youtube上的全屏和最小化按钮,即使用户使用浏览器功能退出全屏,最小化按钮也会更新。你们能帮助我吗?感谢。

这是正在替换的html输入按钮:

<input type="button" value="Full Screen" class="full-screen-btn"/>

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:0)

我在javascript中使用keyup Event找到了答案:

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();
        }
    });
}