02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Introducing the HTML5 classList

HTML5 comes not only with new video players and new fancy features it hides within it easier ways to interact with CSS and easier ways to interact withing HTML itself. It's time for us to take a peak at one of the really fun ways to interact with CSS. So let's jump in and see how to work with classlist.

Video Transcript

00:00Alright, so let's take a really slight break from the video core functionalities to learn
00:04about some other features in HTML5 that could relate very well with videos.
00:10So one thing that I personally really like is when I play a video, it's really frustrating
00:13when there's like a really light background in the background like on YouTube.
00:18Very very annoying, especially in the middle of the night, it just adds too much light
00:21in the room forcing you to go full screen and sometimes you just don't want to go
00:25full screen.
00:27So let's add a really fun functionality where when someone plays their video, it would automatically
00:31dim the background and whenever they're pausing the video, the background will return to the
00:36way it was.
00:38To do that, we're going to leverage a few new HTML5 styling functionalities that were
00:44added into HTML5.
00:45So let's see how that is done.
00:47So to do that, first of all, what I want to do is I want to add new styling.
00:50So I'm going to go into my style area and I'm going to create a gray area.
00:55I'm just going to call it gray and in the gray, all I'm going to do is I'm going to
00:59add a background color and I'm going to set that to be a dark gray.
01:03So we got here.
01:04Yep, we got here a dark gray.
01:06And next is what I want to do is I want to dynamically change my body's background
01:11based on if we're playing or not.
01:13So whenever we're playing, what I want to do is I want to go to my document body and
01:18I want to actually go to its class list with this is a new functionality inside of
01:22HTML5 and I'm basically asking for the class list.
01:27And after I got the class list, I'm basically telling it I want to add a new class to it.
01:31The class I want to add is that new class that we just created that was called gray.
01:36Next I want to do the exact same thing when we're paused only instead of adding, you guessed
01:41it, we want to remove it.
01:43So once we add and once the video pauses, we want to add a remove.
01:49So we have an add and we have a remove.
01:51Once the video is playing, we want to add that gray layer.
01:54Once the video is paused, we want to remove that gray layer.
01:58So let's go back into our browser and I'm just going to click on refresh to make sure
02:04that we got the latest functionalities and again, I'll wait for that ready state.
02:08Once the ready state is ready, I'm going to click on the play and notice how the
02:11background changed the color.
02:13And once I click on the pause, the background will be reset again.
02:18So you could see how fun and how easily you could create very smart video players, even
02:24with basic functionalities of the video player by adding enhancements that a basic player
02:29just wouldn't have and making your video player tie in more deeply with your own website.
02:34All right, so now that we got the video play and pause functionality set, I want to talk
02:38just for another second about one more feature, one more component or two more components
02:44actually that the class list has, it has two more methods.
02:48And I want to explore them for a second.
02:50So I'm just going to do that here up here for a second.
02:53And we saw that we had the add and we had the remove.
02:57But another two functionalities that we have is one is the contains.
03:02So contains basically will return contains will basically return a boolean value console.log.
03:11It will basically return a boolean value telling us if this body contains that style or not
03:17in that style, basically, does it contain the gray class.
03:21So I'm going to click on Save and again, move into our browser and click on refresh.
03:25And now we should automatically get the value of false because initially when we start,
03:29it does not contain that.
03:32Now we have another way of approaching it, which is one is a toggling a class on and
03:38off.
03:39Now the way we toggle on a class on and off is by assuming that automatically, and this
03:44is kind of a fun feature is we're assuming when our application first starts is that
03:49there is no gray class on it.
03:51If we wanted to, we could have assumed that every time we press on the pause or play
03:57button, the gray or ungray would be undone.
04:01Or we could have another really fun functionality is we could create another button.
04:06And let's say we could call this dim light.
04:11And the dim light function could be a dimmer.
04:15We could give it the ID of a dimmer.
04:17And now I could actually add another separate controller that would be a dimmer.
04:23So I'm just going to copy what we have there and change that to our ID to dimmer.
04:28And this time around, we will call toggle dim, dimmer, toggle dimmer.
04:33And we could create a new function.
04:35I'm just going to scroll up so you could all see easily.
04:37So first of all, just to see we created the same get element by ID.
04:41We call the toggle dimmer.
04:43So we want to create that function.
04:44So I'm going to create the function toggle dimmer.
04:49Now that I created the toggle dimmer, let's see how easy it
04:52is to actually toggle the functionality.
04:54So I'm going to call the document.body class list.
04:59And I'm going to call the toggle function.
05:02And in the toggle function, I'm just going to say basically toggle the gray.
05:07Basically meaning that we might add it.
05:09We might remove it based on basically the scenario that it's in without needing
05:13a case, a two case scenario.
05:14Obviously, in our scenario, this is probably our favorite approach.
05:18Or we might create two separate buttons, one to control the dimmer,
05:22while one to control the video play.
05:24And you could do either or.
05:25So if we go back into our player after we've done that and saved our file
05:28just to see that everything is still working,
05:31we'll see that we have a dim light functionality.
05:33And again, we're just going to wait for it to be ready.
05:35And when we click on the dim light, it should automatically
05:37change the functionality of the dim.
05:40Now, only problem that we have here is that maybe once we play,
05:44but it shouldn't affect it because even when we're playing,
05:46it should automatically keep the gray because we automatically add it.
05:50And if it was already there, then it's not going to add it again.
05:52Once we click pause, though, it's going to automatically go to the default state.
05:57And again, one of the things that you could do
05:59is add another layer of complexity, such as making sure
06:03that you store the latest state that was there before you change the state.
06:09So for example, you might have wanted to create a global variable.
06:13And the global variable would be something like dimmed state.
06:20And dim state would be a Boolean value.
06:23We could call it maybe is dimmed because that's a much easier read.
06:28And the value initially is false.
06:30And we could basically toggle that value each time we change something.
06:34For example, in our dimmer, every time we change the dimmer,
06:37we could set it to be the exact opposite of what it was.
06:40Basically saying that we always know what was the original state of the dim.
06:45Now that we know what the original state of the dim is,
06:48we could always then, in the state, basically ask here.
06:52Instead of doing the remove automatically, we could ask if is dimmed,
06:57then we probably don't want to do anything.
07:00But if it is not dimmed, then we want to remove it.
07:03So if it's not dimmed, if our basic state is not dimmed,
07:06then we want to remove those functionalities.
07:08And obviously, there might be a few bugs here
07:10if you click really fast between the buttons now.
07:13But this is the idea of creating a more sophisticated and more complex video
07:17player.
07:17So now once we go back into our editor and I click on Refresh,
07:21and this time I'm going to start off by being dimmed once we're ready,
07:24I'm going to click on that dim so we start playing once we're dimmed.
07:27And I pause the play, it's not going to change our dim state,
07:30and it's going to remain in the state that it was.
07:33We could then turn off the dim, play again, turn off the dim again.
07:37And then once we pause and then play again, every time we play,
07:41it's going to go back into that default state.
07:43But every time that it's paused, it should go back to what it was.
07:46So for example, now we're dimmed out, now we're playing,
07:49and now we pause, and we go back into that initial state.
07:52Now, I didn't test and troubleshoot and make sure that it's 100% bullet
07:56proof, and I'm quite sure that if we click, click, click, click back
07:59and forth, we will create an irregularity between those behaviors.
08:03But overall, it's going to be quite stable.
08:05And obviously, you are more welcome to polish it
08:08and confirm that it always works.
08:09I could already see one scenario where it wouldn't work,
08:12but it could be easily solved, but there
08:13would be a little bit out of scope of the topics
08:15that we're trying to cover right now.
08:17So I encourage you to, as homework, to solve this problem
08:20of the error, not error, but the misalignment of the buttons
08:25so you'll be able to make sure that they always work even when they're two.
08:29So that covers that, and let's continue on learning and exploring video players.
08:35Thank you.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!