Placeholder Image

字幕表 動画を再生する

  • Hello, everyone.

  • In this video, we're gonna build out a fully animated, light dark themes spot for using mostly CSS and a little bit of JavaScript.

  • And this video is gonna teach you a bunch of great concepts for some advanced CSS as well as how you can use SPG inside your CSS.

  • So let's get started now.

  • Welcome back to Webb.

  • Have simplified my name's Kyle, and my job is to simplify the web for you so you can start building your dream project sooner.

  • So that sounds interesting.

  • Make sure you subscribe to the channel for more videos just like this one.

  • Now, to get started, I have a completely blank visual studio code project open and on the right.

  • I have the final version of our project, and as you can see, it's a very simple project.

  • We have a picture of a son and then a swap themes button, and when we click that button, that sun is gonna go off to the right side of our screen and the moon is gonna come in from the left side of our screen, and everything is gonna smoothly transition to the dark name and then movie click swap things again.

  • It's going to do that same thing.

  • Moon jumps off to the right son, comes in from the left and everything slowly, transitions backed to this light theme.

  • We can click this button as many times as we want, and it's going to swap between those different themes.

  • So to get started with building this, what's first create an HTML file, we'll call index dot html and we'll just have exclamation point enter to generate all this boilerplate code and all this bully re plate with the exclamation point is generated using Emmett.

  • And if you want a full in depth tutorial on Emmett, I have one linked in the cards and the description down below.

  • Now, to get started, we're gonna need are different icons for the moon, this icon down here with the moon slash son and then finally our son icon and I got all of these from this website called material designs icons dot com.

  • So if we just search for Moon here, we can see Here is our theme light, dark and in order to get these into our CSS and are hte e mail because, as you can see here this icon actually changes color.

  • It's swapping colors as we rotated around.

  • So we actually need to be able to modify this inside of our CSS.

  • So what we want to do is actually grabbed the SPG.

  • And if we just copy all of this code for the SPG and paste it into our HTML, this is actually code that we can modify with CSS so we can change the color of this icon.

  • Change the size of it, changed the scaling.

  • We can do everything we want, all because we have this SPG directly inside of our HTML.

  • So let me just put a comment above this so that we know this is our swapper icon.

  • And then let's pull in our moon icon, which is right here.

  • So we just want again get the spg for that.

  • Make sure we copy this.

  • Come down here little ways and we'll give this one a comment of Moon.

  • Make sure this is indented properly and we'll do one more.

  • This one is going to be for a son.

  • And if we come in here and we just search for son real quick, it takes a little bit of time, and we have our son icon right here.

  • So let's just click on this, grab it as an SPG, and we want to just copy that over.

  • So now we have all over different icons inside of our HTML here.

  • You're gonna fix this space.

  • Seems that everything is perfectly spaced out as we want it.

  • And we can close that.

  • We don't need this site anymore.

  • So now that we have our different icons, let's work on the HTML that goes around these icons in order to get these different styles that you want.

  • The first thing we need is we're gonna need some form of title.

  • So let's come into our body.

  • What issues?

  • An h two for this, and we'll call it a theme swapper.

  • And let's open this up using live servers so we can see what we're working with.

  • And as you can see, we have dreams swapper, and there are three different icons down here.

  • So the next thing we need to do is create this button, which is going to have the text swap themes as well as our icon.

  • So we'll create a button and inside of this button, we're going to have that icon for this swapper.

  • So just copy this SPG and paste it into here again, fixed this indentation issue.

  • And there we go, that's fixed.

  • And also to make this easier to work with.

  • I'm just going to condense this down so that it's a little bit smaller.

  • And I can also put the text swap themes, hopes, themes.

  • Now, if we look over here, we have that button with the icon and our text swap themes.

  • Now, lastly, we need some kind of container which is going to contain our son and our moon, because the way that we're actually going to make these rotate off the screen when we click the button is by putting both the sun and the moon in the same container and position 1 10 pixels away from the top of the screen and the other one way down at the bottom of the screen.

  • So that way it'll just perfectly rotate to the exact right spot that we want it.

  • So we're gonna put them both just inside of the same container for now, So let's just create a diff to do that.

  • Delete all these comments, and we could just have a div inside of here, and we're gonna put our moon, which is this first icon here.

  • And we're gonna have our son as well again.

  • Let's just fix this indentation lives.

  • And there we go.

  • And we're gonna copy this son in there as well.

  • Just get rid of all this extra comments.

  • We don't really need them and will pace the sun in.

  • There is the very 1st 1 again, fixed this just like that.

  • And again, I'm gonna put that comment back in there for Sun, and then we can have the 2nd 1 down here.

  • Say, Moon, just so we know which one is which one.

  • We need to go change them later.

  • So that's all the html that we're going to need.

  • So it's actually now work on adding a few classes so we can start styling these inside of our style of sheets.

  • So the first thing we need to do is to link a style sheet.

  • What you gonna come in here?

  • Call it styles that CSS.

  • I'm gonna shrink down this side of the browser so we can easily see the code we're working with.

  • And also the next thing that we're going to need to do is add in a class on our button.

  • So we're gonna come in here, we're gonna add a class, we're gonna call it theme ta go button.

  • And on this SPG here, we can actually add a class to this and we'll call it icon so we can actually modify the SPG and change its color as we change the buttons color.

  • Next thing we're gonna do is this, son, we're gonna add a class here, which is just going to say, son, and we can remove that comment.

  • Same thing for the moon will at a class which says moon and we'll remove that comment since we now know which one is which.

  • Lastly, we want that container which contains both our son and our moon, and we're gonna call it aptly named our son Moon Container.

  • So now we have a bunch of styling for our classes created, so that's great.

  • Now we can move on to actually styling this with our CSS.

  • So it's create that styles dot CSS file and the way that I'm going to switch between light and dark mode is by adding a class to our body.

  • So if we have a class which says dark, then we should be in dark mode.

  • And if there's no class than it's going to be light mode by default.

  • So let's just get rid event and inside of our styles we need to remember that.

  • So we're gonna have a body, and we're also gonna have a body dot Dark, which is going to be for our dark mode.

  • And the way that we're gonna swap between these two modes easily is by using CSS variables.

  • So we're gonna set the variables inside our body and then overwrite them in our dark mode for our dark theme, which has that darker background and this yellowish color.

  • So now, with those two classes created, let's first work on actually styling this sword theme.

  • Swapper and button are dead center in our screen.

  • So let's go over here, see what we have to work with.

  • And the first thing we're gonna want to do is use display of flex would justify content in the center and a line items in the center.

  • This is going to allow our content to be centered and in order to make our content show top to bottom We just want to change the flex direction to be column now everything will be stacked on top of each other and you'll notice it's centered horizontally.

  • But we also want to send her vertically.

  • So we need to make sure we specify our height for our body, which will use 100 VH, which is going to be 100% of screen size.

  • If we say that you notice this is in the center.

  • But we have this annoying scroll bar.

  • So if we remove the margin on our body, it's going to get rid of that school bar, and everything is gonna be dead center exactly like we want it now.

  • The next thing to work on is going to be the colors between our different themes.

  • So let's go over here and look at what our light theme looks like because this is going to be our default body.

  • So inside here, we're gonna have a few different variables.

  • We're gonna have a variable for accent color.

  • This is going to be this orange color and this yellow color.

  • So between the different themes and this accent color for default is just orange red, just the default html orange red color and we're also going to have a background color.

  • So it's created background color.

  • In our case, this is just white and we're also going to have a text color which in our case here is just going to be black.

  • Pretty straightforward.

  • But we're also going to have a button text color.

  • So it's create that is, well, button text color.

  • This is what color is This icon as well is this button text and that in this case, is going to be white.

  • Now, the last thing that we're going to have is the amount of time our animation takes.

  • As you can see here, this takes one second to transfer between one state to the next day.

  • So we're gonna create a transition delay, variable hopes, transition delay.

  • And this is just going to be set to our delay, which is one second in our case, and you can change this.

  • However, you want to make it either quicker or slower animation, depending on your needs.

  • So now inside of our body dark.

  • Let's overwrite some of these colors to make sure that the colors we want for this dark thing so the first thing we want to do is this accent color in our case is accent color is going to be D zero d zero hopes d zero d 066 And that's going to be this yellowish color that you see over here.

  • Next thing we're going to have is our background color, and this is just going to be 333 and then, lastly, we're going to have that text color.

  • And this text color here is going to be white in this scenario, and we also want to make sure that we set the button text color.

  • So we're gonna go in here and say, button, text color is going to be 333 And one thing that you'll notice is that our background color and our button text color are both the same between our body and our body Dark.

  • So it's actually just set our button text color here to be equal to our background color, and we can even just remove that down here that way, once we change our background color are button text color is goingto update to reference that background color.

  • So now let's start using these variables we can get started by saying in here our background color is going to be set to that variable so far, backgrounds color.

  • We're also going to set our color here equal to our text color, so we'll save our text color.

  • And now let's look over here and you can see as before we have white and black, so there's really not much difference here.

  • But if we come over and at a class of dark to our body, so we just say dark here and save you can see.

  • Now we're getting white text and we're getting a blackish colored background.

  • So we know that our light dark mode is working for a background as well as our color.

  • The next thing to work on is going to be this title.

  • We want to make it just so it doesn't take up quite as much room with a little closer to our button.

  • So it's at a class to that title.

  • We'll just say Title was going to be that class, and instead of here we could just select that title and we're gonna remove the margin so it's a margin.

  • It's going to be set to zero and then we'll add a little bit of margin on the bottom.

  • We'll just say 00.5 r E m just a space our text out from our but in just a little bit.

  • And I think that already looks a little better.

  • Next thing to work on is going to be this accent color as well as this button text color.

  • So it's select that button, which we called theme ta go button.

  • We're gonna set the background color just to be equal to that accent color, and if we save, you can see we're immediately getting that yellowy colored button.

  • We can also change our color here to be equal to our button text color.

  • And if we say if you can now see that color of our icon as well as the color of our text here, has changed to be that button text color.

  • And if we remove dark from our title there from our body here and save, you can see her button is orange with white text.

  • So we know that our theme swapping is working now.

  • The next thing to do is actually style the rest of our button, So the first thing I want to do is change our display here to be flexed.

  • And that's just so that we can justify our content in the center and a liner items in the center.

  • And that way all of the text inside of our button is dead centered.

  • And this swap themes lines up centered with the icon on the left hand side.

  • Also, we're going to change it so that our cursor here is the point, their cursor.

  • So we know that we can click on this, and we're also going to change around some of our padding and border.

  • So I had a little bit of a padding 0.5 e m and 1 p.m. just to make it a larger button, and we'll do a border radius here, which is going to be 0.3 e.

  • M.

  • Also, we're gonna remove that border around it because I don't really like that border.

  • Look where you're gonna have a flat looking button here, which I think looks pretty good and lastly, which can remove the outline from it so we don't get that annoying blue looking outlined.

  • But because we remove the outline, we need to make sure that we come back in and had a hover and focused it.

  • As you can see here we have a hover focus state, which is going to be this enlargement of the button.

  • And even if we tap to it, it's going to a margin.

  • So we need to make sure we add that into our button over here.

  • So it's obvious to the user that they're hovering it or going to it, that they can click on it.

  • So to do that, we're just going to use a simple transform.

  • So in our theme toggle button, whenever we hover over it or for a theme toggle button, whenever we focus on it, we're going to do is take our transformed property and we want to set a scale here to be a larger scale will just say 1.1, for example, so it's going to be 10% bigger, and now when we hover, you can see our button becomes 10% bigger.

  • But it's doing so immediately.

  • We want this toe happen slowly with that transition duration, which we to find appear instead of our variables.

  • So in order to do that, we could just set a transition property on our theme toggle button on what's gonna set it to that variable, which is transition delay.

  • And now you can see it's going to slowly animate in an animate back out and to make sure that our animation works properly in all different directions.

  • Let's just make sure we set a default transform with a scale here of one that we know for sure everything is going to scale in and out exactly as we want.

  • Also, this transition is going to make sure our color transitions also so that if we change our color from light to dark, it's going to ease in and out of that color.

  • And we can actually see that by starting to add some JavaScript to our application.

  • So it's just create a script dot Js and in our index that age Tamil, let's make sure we import that script and we're going to just say inside of here script dot Js And if we make sure that this is a defer, it means it's going to load after the rest of our HTML, which is essentially the same as putting this at the bottom of your body.

  • I just prefer to use Differ.

  • So now, with that done inside of our script out J s, we can actually work on toddling out our light and dark theme based on when we click this button.

  • And this is actually pretty straightforward.

  • We just want to do document dot query selector and we're gonna get that theme toggle button.

  • So it's a theme toggle button, and we want to do is add an event listener.

  • And whenever we click on it, we want to run a function.

  • So we're gonna run inside of here a function and all this is going to do is get the body of the document, get the class list, and we're just gonna toggle that dart class.

  • So now every time we click this button, you can see our dark classes ta going on and off.

  • And as you can see, this button is slowly transitioning our text color between light and dark.

  • But our background right now is really quick does change and same with our text color.

  • So, in order to fix that in our styles which just go into our body and what we're gonna do is just adding a transition training position and this transition is just going to be our transition delay just like that.

  • And now if we save and we spot our themes, you can see all these colors really nicely fade in and out of each other.

  • Which books?

  • Really good.

  • The last thing that we have left to do for this button is to just space out our text from our icon.

  • And if you remember right, we created a class called Icon, so we can just select the icon inside of our button and handsome margin on the rights of 0.5 e.

  • M.

  • Now, as you can see, we have a little bit of space between our icon and our text on the right, which looks really good.

  • Now, the last thing to work on is going to be this sun and moon transition.

  • So it perfectly circles around our application, as you can see here.

  • So it's first select the container that they're inside of this sun moon container and inside of here, in order to easily make them rotate around like this, we're just gonna position this.

  • Absolutely.

  • So what's coming here?

  • Do a position of absolute and if we save immediately, can see they're just set in the center of our screen, and what we want to do is make sure this takes up the entire size of our screen.

  • So put the top of zero.

  • So that's going to start all the way at the top.

  • And the height is actually going to be something that's slightly different than the entire height of our screen, where we're actually going to do set the height to 200 V Men and V men.

  • Essentially, what it does is it selects whatever the minimum size is in view, so either with or the height with ever shorter, and it's going to be 200% of that with where height.

  • So in our case, this is 200% of our with because our wit is the smallest size, and the reason we were using the men instead of the high Teer aur VH is because we want to make sure that this transition looks good on all different screen sizes, whether they're narrow or whether they're wide.

  • And this is because if we did the height, this would be a very large container versus the actual width of our screen.

  • So instead of having a nice circular path.

  • It would actually have a really ob long kind of oval shaped path, and we wanna have more of the circular shaped path.

  • This is where we're going with the men here instead of the H.

  • Another thing you'll immediately notice is we no longer can interact with our button very well, as you can see what I can in that cursor pointer, and only when we suffer in certain spots are we able to interact with it.

  • And that's because this Sun moon container is actually covering our entire screen.

  • So what we want to do is make sure he's set pointer events to none.

  • And this essentially prevents the mouse from ever interacting with this sun moon layer, which means that we're always able to interact with the button underneath of it with no issues.

  • Now, with that out of the way, you'll notice another issue, and that's that We have this dreaded scroll bargain.

  • That's because the sun moon container is larger than our entire screen height.

  • So in order to get around this issue, what we need to do is go all the way to our body, and we just want to set here.

  • The stuff that is overflowing our container, we want to hide it so well.

  • Say overflow of hidden.

  • And now anything that overflows no longer shows up on our scroll bar is gone.

  • Since we can't actually see this content, such is hidden now back here to our sun moon container.

  • In order to make these elements rotate around our screen, we're gonna use the transform.

  • And we're actually gonna set the rotate here to some form of variable, which is going to be our rotation.

  • And we're just gonna set that variable up here to be lips rotation just like that.

  • And we're gonna default this to zero because it died default.

  • This son is going to be at the top now in order to get our icons in the correct position.

  • Let's select both our son and we're gonna select our moon.

  • And we're also gonna create the joint selector that selects both our son and our moon because there's quite a bit of stuff we're gonna share between them.

  • For example, they're both gonna be absolutely positioned, and the sun, for example, is going to have a top of 5%.

  • And the moon is going to have a bottom of 5%.

  • And if we say you can see our son is here at the top 5% and our moon is actually off the screen because it's 5% from the bottom of this 200 demon container.

  • If we shrink our screens small enough, you'll see the moon pop back up.

  • Isn't that 5% away from double the with?

  • But most the time Our screen is gonna be whiter, so the moon is almost always gonna be off our screen.

  • Also, you notice these icons are slightly offset.

  • They're not quite center.

  • So to fix that again, we're just gonna use fucks box.

  • So we're gonna change this to display flex, justify the content in the centre, aligned the items in the center and if we save now we have our icon nice and centered in the screen and the same thing with our moon.

  • It is also centered inside the screen and in order to make it so that this moon does not appear.

  • Even if our screen size is small like this, we're actually gonna set the capacity to be zero.

  • As you can see, that moon has now disappeared.

  • But when we're in the dark mode, so dark dot moon, we want our opacity for the moon to be set toe one.

  • And our son here is going to have a capacity of one in light mode.

  • But when it's dark dot son, we're gonna do is set that opacity to be equal to zero.

  • Now, when we changed the dark mode, you can see that our moon is showing up and when we're in light mode, you can see our son is showing up.

  • And in order to make these smoothly animate in their opacity, we want to make sure we set a transition.

  • And this transition is going to be here for our transition delay.

  • So we'll say transition delay.

  • But we only want to transition.

  • For now, Rapacity hopes opacity just like that.

  • So this will only transitional rapacity and none of the other properties of our icons.

  • Now, the last thing to do for these different icons is we'll make them slightly bigger.

  • We use 30 pixels in the whip, and the height is also gonna be 30 pixels.

  • Just so they're a little bit larger on the screen, and we're also gonna set the Phil Phil for an icon is essentially the same.

  • It's setting the color for text.

  • It's going to set this color for icon, so we want to set our Phil here to be equal to that accent color.

  • So it matches our buttons.

  • And as you can see, our icon actually didn't change color.

  • So if we go over the index dot html, you'll notice one thing about these past is they have their fill value already set.

  • Let's just remove this.

  • We don't want this to fill.

  • Based on the current color.

  • We want to manually set our fill ourselves.

  • So if we remove that from our son and our moon and save, you can now see our icons have the correct color.

  • And when we swapped themes, you see our moon icon has the right color, and our son icon has the right color.

  • Now, to make sure the transition of our colors is seamless, let's make sure we add in Phil toe our transition.

  • So now our colors are going too smoothly transition between the accent color that is for light and the accent color for dark.

  • Now we're finally onto the last step, which is handling the rotation so that our sun and moon icon properly rotate in and out of the screen.

  • And we're just gonna handle that first the naive way, and then we're gonna look into the better way to handle the problem.

  • It's the first thing you may think to do is just whenever our screen is dark, we want to set the sun moon container tohave a rotation here, which is going to be with US rotation, which is going to be 180 degrees.

  • And if we save and we switch our theme, you can see that our moon icon is now at the top.

  • And if we spot back, our son icon is at the top.

  • So we're properly doing our rotation.

  • And if we actually set in transition here, which is going to be four are transform.

  • Lips transform, and it's going to be over that transition duration.

  • And now we save.

  • You can see that is properly rotating in, but when we actually go to swap teams back, it's rotating the wrong direction.

  • We want to constantly be adding 182 are degrees every time, so they're constantly rotates to the right every single time.

  • Also, you'll notice our moon icon is upside down.

  • So to fix that instead of our moon will be condemned because we can just change the transform.

  • We want to change the rotate to be 180 degrees.

  • That way, our moon is always gonna be right side up at the top of the screen, and it's going to be upside down at the bottom of the screen.

  • But that really doesn't matter, since it'll be invisible.

  • So in order to fix this issue, what we're gonna do is actually set our rotation in JavaScript by adding 182 the current rotation, which means that we wanna handle this degree portion inside of our CSS.

  • So we don't have do string parsing inside of our JavaScript.

  • So just do a cow here.

  • Will we multiply our rotation times one degree?

  • This is going to convert a value of, for example, 1 80 into 180 degrees for us.

  • And if we save, you can see that this still works the way that it was before, with the left and right.

  • So for remove this and jump into our job a script we can actually get that Sun Moon container Soldiers say Sun Moon container is going to be equal to document that query selector of that class of sun, moon container lips, moon container just like that.

  • And now we want to do is first get our current rotation and this is going to be from the sun moon container.

  • We need to get the computed style so we can call this function called get computed style passage in the element we want to get the computed style of and then we can say good property value.

  • And here we just pass in the property.

  • We want, in our case, that this is our rotation and this is going to come back to us is a string.

  • So we need to parse this into an imager so we could just use the parson function and now pass it in that computed style which we just calculated, and that's going to give us an imager, which is our current rotation.

  • Then we can set our new rotation like doing that same thing with our sun moon container.

  • We want to get the style property and we want to call set property and in here we pass of the property, which, in our case of his rotation, and they're gonna pass it our current rotation, plus 180.

  • So it's just going to add 100 and 80 degrees onto our current rotation.

  • Make sure you spell rotation here, and if we save click swap themes, you see it go to the right and again go to the right.

  • And every time we click, this is just going to keep going to the right because we're constantly adding 180 degrees instead of adding and then removing.

  • So it's constantly going to be moving to the right, as you can see here.

  • And that's all there is to creating this fully animated thema Ta go!

  • If you enjoyed this video, you can check out my other project based videos linked over here and subscribe to the channel for more videos just like this.

Hello, everyone.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

How To Build An Advanced Light/Dark Theme Toggle

  • 2 0
    林宜悉 に公開 2020 年 03 月 28 日
動画の中の単語