Detecting if a browser is in full screen mode

后端 未结 17 1897
误落风尘
误落风尘 2020-11-27 06:10

Is there any way of reliably detecting if a browser is running in full screen mode? I\'m pretty sure there isn\'t any browser API I can query, but has anyone worked it out b

17条回答
  •  甜味超标
    2020-11-27 06:40

    This is the solution that I've come to... I wrote it as an es6 module but the code should be pretty straightforward.

    /**
     * Created by sam on 9/9/16.
     */
    import $ from "jquery"
    
    function isFullScreenWebkit(){
        return $("*:-webkit-full-screen").length > 0;
    }
    function isFullScreenMozilla(){
        return $("*:-moz-full-screen").length > 0;
    }
    function isFullScreenMicrosoft(){
        return $("*:-ms-fullscreen").length > 0;
    }
    
    function isFullScreen(){
        // Fastist way
        var result =
            document.fullscreenElement ||
            document.mozFullScreenElement ||
            document.webkitFullscreenElement ||
            document.msFullscreenElement;
    
        if(result) return true;
    
        // A fallback
        try{
            return isFullScreenMicrosoft();
        }catch(ex){}
        try{
            return isFullScreenMozilla();
        }catch(ex){}
        try{
            return isFullScreenWebkit();
        }catch(ex){}
    
        console.log("This browser is not supported, sorry!");
        return false;
    }
    
    window.isFullScreen = isFullScreen;
    
    export default isFullScreen;
    

提交回复
热议问题