Detect fullscreen mode

前端 未结 16 2160
谎友^
谎友^ 2020-11-28 08:01

Modern desktop version of IE 10 is always fullscreen.

There is a living specification for :fullscreen pseudo-class on W3

But when I tried to det

16条回答
  •  旧时难觅i
    2020-11-28 08:31

    Reading MDN Web docs, I like this native method.

    https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/fullscreenElement

    function is_fullscreen(){
        return document.fullscreenElement != null;
    }
    

    or fancier

    let is_fullscreen = () => !! document.fullscreenElement
    

    This method also works when you open developer tools in browser. Because fullscreen is applied to a particular element, null means none of it is in fullscreen.

    You can even extend to check a particular element eg:

    function is_fullscreen(element=null){
        return element != null? document.fullscreenElement == element:document.fullscreenElement != null;
    }
    

    Which only return true if currently is fullscreen and (element is null or element is the fullscreened element)

提交回复
热议问题