Put element into fullscreen in JS

Hi there!

Today I studied How can I put an element into fullscreen mode and I want to share with you How do it ☺️

Primarily, you should know that It works only in events, so you should use request into fullscreen mode in the event function.

So that …

Hi there!

Today I studied How can I put an element into fullscreen mode and I want to share with you How do it ☺️

Primarily, you should know that It works only in events, so you should use request into fullscreen mode in the event function.

So that put container into fullscreen mode use it

const d = document.querySelector("#d")

d.requestFullscreen(options)
    .then(() => {})
    .catch((error) => {})

You see that it is not guaranteed that the element will be put into fullscreen mode.

So, element.requestFullscreen() returned Promise of undefined.

In options you can change navigationUI param on “hide” | “show” or “auto” (default value).

First, I create a function for get fullscreen method, because fullscreen in some browsers works with prefixes only.

function goIntoFullscreen(element) {
    if (element.requestFullscreen) {
        return element.requestFullscreen()
    } else if (element.mozRequestFullScreen) {
        return element.mozRequestFullScreen()
    } else if (element.webkitRequestFullscreen) {
        return element.webkitRequestFullscreen()
    } else if (element.msRequestFullscreen) {
        return element.msRequestFullscreen();
    }
}

Next, You should exit out of fullscreen mode.

For exit out of fullscreen mode used by document.exitFullscreen. I created a function for it with some browser prefixes too.

function outOfFullscreen() {
    if(document.exitFullscreen) {
        document.exitFullscreen()
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen()
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen()
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen()
    }
}

After put into fullscreen will be available document.fullscreenElement. You can check it for the button event of something else. I wrote a simple check function for it

function isFullScreenMode() {
    return document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || null;
}

Funnily I created an example so that you can try live demo.

You can style your fullscreen into fullscreen mode and elements in use by :fullscreen and ::backdrop.

Finally, you can be listening fullscreen with fullscreenchange and fullscreenerror events.


Print Share Comment Cite Upload Translate
APA
vladimirschneider | Sciencx (2024-03-29T08:44:50+00:00) » Put element into fullscreen in JS. Retrieved from https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/.
MLA
" » Put element into fullscreen in JS." vladimirschneider | Sciencx - Sunday July 4, 2021, https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/
HARVARD
vladimirschneider | Sciencx Sunday July 4, 2021 » Put element into fullscreen in JS., viewed 2024-03-29T08:44:50+00:00,<https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/>
VANCOUVER
vladimirschneider | Sciencx - » Put element into fullscreen in JS. [Internet]. [Accessed 2024-03-29T08:44:50+00:00]. Available from: https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/
CHICAGO
" » Put element into fullscreen in JS." vladimirschneider | Sciencx - Accessed 2024-03-29T08:44:50+00:00. https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/
IEEE
" » Put element into fullscreen in JS." vladimirschneider | Sciencx [Online]. Available: https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/. [Accessed: 2024-03-29T08:44:50+00:00]
rf:citation
» Put element into fullscreen in JS | vladimirschneider | Sciencx | https://www.scien.cx/2021/07/04/put-element-into-fullscreen-in-js/ | 2024-03-29T08:44:50+00:00
https://github.com/addpipe/simple-recorderjs-demo