Use requestFullscreen to Make Elements Full Screen
In this lesson, we take a look at making elements full screen using the requestFullscreen method. We also look at how to change styling for elements that enter full-screen mode and how to create custom controls for exiting full-screen mode.
Duration
3 min
Access
Free
Transcript
Available
In this lesson, we take a look at making elements full screen using the requestFullscreen method. We also look at how to change styling for elements that enter full-screen mode and how to create custom controls for exiting full-screen mode.
Video Transcript
Jhey Tompkins: Here we have an image and a button, and we're going to make it so that when we click our button, it toggles full-screen mode for our image.
We use a click event listener and then we're going to call our function toggleFullscreen(). To start, our toggleFullscreen function looks like this. We take our image and we use requestFullscreen(). If we click our button, element.requestFullscreen will be invoked, and our image will become full screen. We can use escape to exit full screen.
What if we want to provide a button to exit full-screen mode? We could wrap our image and our button inside a div, and then make the div go full screen. Instead of referencing the image, let's instead grab the div. If we try toggling full screen again, we can see that our div is now taking up full-screen mode, but our image is no longer full screen.
What could we do to make this image full screen and then the button to stay on top of it, so we could exit full screen? We need a way to detect that we're in full-screen mode. In some browsers, requestFullscreen returns a promise, which is an ideal way to hook into when an element goes full screen.
A universal way to handle this is to add an event listener to the document that listens for fullscreenchange. We could bind an onChange function to that event, and our onChange function will toggle a class on our div. We want to apply class of fullscreen to our div, but only when it's in full-screen mode.
To detect that, we can check for the existence of document.fullscreenElement. If it exists, we apply the class of fullscreen, else, we apply nothing.
Here we've got some extra styles for when we're in full-screen mode. Our image should take up the entire viewport and the button should be absolutely positioned at the bottom, but in the middle. Let's try it out and see what happens. Now our image takes up full screen, and we can still use our button, but our button isn't currently hooked up to exit full-screen mode, so let's do that.
Inside toggleFullscren we can add an if conditional, and we can check for document.fullscreenElement. If there is a fullscreenElement, we want to exit full-screen mode by using document.exitFullscreen. Else, we requestFullscreen for our div.
Let's try that now. If we press our button, our div goes full screen, and our image takes up the full screen, and we can click our button and it exits full-screen mode and the styles return to normal. We can also try with DevTools open and we can see that our fullscreen class has been applied to our div. When we close it, it's taken off.
In review, we can use requestFullscreen to take our elements into full-screen mode. In some browsers, this method returns a promise. If we'd like to hook into changes, we can use the universally compatible fullscreenchange event.
To determine whether we're in full-screen mode or not, we can check for the existence of document.fullscreenElement. When we're ready to come out of the full-screen mode, we can use document.exitFullscreen.