- Use
Document.querySelector()
andElement.requestFullscreen()
to open the given element in fullscreen. - Use
Document.exitFullscreen()
to exit fullscreen mode. - Omit the second argument,
el
, to usebody
as the default element. - Omit the first element,
mode
, to open the element in fullscreen mode by default.
const fullscreen = (mode = true, el = 'body') =>
mode
? document.querySelector(el).requestFullscreen()
: document.exitFullscreen();
// Examples
fullscreen(); // Opens `body` in fullscreen mode
fullscreen(false); // Exits fullscreen mode