 The other day I was trying to solve a challenge that my friend Amit Sheen had sent me and along the way I learned a couple of new tricks or things I guess you could do with animations and transitions that I never knew about and it got me thinking a little bit that there's actually a whole bunch of neat little things that can help us save time or simplify things or just fun tricks that we can do with animations and transitions that a lot of people aren't actually aware of and I figured it'd be a good idea to make a video about those and so here we are and we might as well just dive right into it and jump into VS code right here and we're going to be starting off this button but then we'll bring some other things in along the way as well to explore the different stuff we can do. So one thing that's super common is to have something where we sort of scale things up or have some sort of transform at least on the button. So here I'm just transitioning it or translating it I should say upwards a little bit when we hover on top it's a lot more than I would actually do so I'm just exaggerating it a bit for this video and then what we often do is put a transition here where we do say like 500 milliseconds and on that transition we'll do our transform we have transition transform translate my mind is getting fried with that we'll just say 500 milliseconds and we get something like that where it just looks a little bit better right where it eases in and out but we can actually make this a bit better and let's exaggerate things here a little bit so let's say when we hover on top it's going to go and you know what I said we're going to exaggerate this let's really exaggerate it so when we go on top it really moves up and then it moves back down there and this is completely fine but we can add a little bit of more interest to our animations by actually having different ones depending on the state so I'm going to copy this right here and we're going to put a different transition when we're hovering and now let's just say this is 250 milliseconds so when I first hover on top it goes really fast button's excited we're going to click it and then we let go oh right so we sort of get the little the little let down and I think it adds a little bit more excitement that way so this is when we're hovering on top and then the regular one when we're not and this is something that I actually do quite a bit I think it's a nice fun way to have a different speed a different timing function different things going on sometimes we're only transitioning something in one direction but not the other and it can be a nice very useful thing to use on your projects now next I want to look at some animation stuff rather than transitions and I've just set things up to add a class of party time when we click on our button and over here I have an animation with some keyframes going on so let's say that class of party time party time we can do our animation of a party and we'll do and we'll say 2000 milliseconds so it's not too fast and we'll say infinite like that so now if we come and click on it we start getting our party time going on and if I click it again it will stop so that's perfect and it looks you know pretty good it looks terrible but anyway um but the first thing I want to look at here is actually how we can change the order of these and it won't really matter and you know what let's just simplify things a little bit by only having um look where we red orange red but let's take this red here and put it above so we're doing we have red red orange but we have a zero percent here 40 percent then 20 percent so if I save this and we click it's still going red orange red and then we you notice you know that that orange is still coming in here so or even you know let's just make this green so we can see that it's actually going to go right we get the green then we get the the green first and then it's followed up or sorry we get the orange first and then after that the green and maybe we'll make this a little bit slower just to make it a little bit more obvious so we get the orange first then we get the green even though the green is declared first because it's not the order that's important it is the percent along in the animation that is important well let's click on that to stop it for now so it doesn't bother us too much and let's come in here and actually delete a whole bunch of these and something that's interesting is let's put this at 50 percent or maybe we'll do it at 25 percent it will go to yellow uh but let's say I wanted another red to come in I could actually come here and say 50 percent it's going to be red then here I could say at 75 percent it's going to be yellow and then at 100 percent we can come in with one more and say that the background color on the last one here let's just choose this because it's showing oh it's a little bit too close to red actually uh let's make it 200 so it's more in the blues and now we can click on that and it's going to cycle through and it's going to do all of them before going to the blue at the end there so it's doing zero then the 25 then the 50 then the 75 here so we're combining different keyframes with one declaration and this can be useful just to if you have a very simple animation that just has a few things in it instead of having to have a whole bunch of keyframes you might be switching between two of them like we're doing here uh whatever it is you can combine them no problem at all and another thing you could actually do here is let's get rid of all of these and at 100 percent let's just or we could keep it like that and that's not going to be obvious enough so let's make this one 300 so we're back into like the purples and we'll notice that the color is going and then it's we're on infinite so it gets to the end and then it starts over we're not alternating through but you'll notice the 100 point and I have nothing else declared and it's using the zero percent as just the regular default that we are using here which was a variable I have set up that is clearly blue so you don't actually have to declare any starting point and maybe you have a whole bunch of different things they all get pushed in one thing so it could be a transition for you know that translating or something or whatever it is you don't have to worry about the starting point you can just worry about the ending point or I could do this as a 50 percent and then it would actually go and then come back so we get the purple and then halfway through the animation and then it goes back to the default color after that so just having a 50 percent like that sometimes can actually be really useful now for this next one we're actually going to jump over to code pen because this is the challenge and this is one of those things I found out doing the challenge that I never would have expected to be honest and it was to create this effect right here where it slowly speeds up it just keeps on spinning and then it slows down when we come off and it's not using any javascript to be able to do that and the way this is basically working is I have this animation that's going so we have it right here and we run that animation when we hover so when I hover on top it's going to start doing the spinning and it's doing a transform to rotate it 360 degrees but we want it normally that would just sort of go at full speed from the very beginning which we didn't want and this is where I found out you can actually do a rotate so we have like rotate transform and scale are their own properties now do check with browser support if you do plan on using them but basically what I'm doing is on hover I'm reverse rotating it and we have a transition on that so it's going from negative 720 to zero so it's negative rotating while the other thing is going but that negative rotation is actually slowing down as that's going on and then at one point it's not running anymore and then we get the thing at full speed and then when I come off of the hover it sort of the opposite effect is happening I don't know why you'd necessarily want to do this in the real world but the transform rotate and the actual rotate can be used as two separate properties counteracting each other in certain ways this is the use case I found for it if you'd like more watch the video where it's a little bit more clear on exactly how that's happening because at first I did it with a parent but anyway just something that I thought was really interesting so I wanted to share it as part of this video all right so for this next part I've added a whole bunch of dots all along here and those you can see them right there they're red have some basic styling coming on them so we can see them and I've also added this dot dance with this very simple animation on here that's just making them rise over two seconds using this and we want them to actually be dancing rather than rising and I have updated the JavaScript that just toggles that so when we click the button or the dots rise and fall back down because we are running it as an alternate so there we go that we have them bouncing up and down basically and one way you can make it a little bit more interesting is using animation delays and because we have 15 dots the easiest way to have a different animation delay on each one of them is to use sass here so I am using a sass for loop that's just going through each one if you're not familiar with sass basically we're saying four i so the variable i here from one through 15 so the first time it does it this will be nth child one like we have right there 100 milliseconds times the number one so 100 then it's going to do it again for the second one so this becomes nth child two 100 times two so we get a delay of two then a delay of three then a delay of four then a delay of five all the way up but we can write a lot less code to make it happen so now if I click you'll see that it starts and they all start going up and down and it looks kind of nice actually I think starting off like that and let's just click to turn it off and we'll do that again but you'll notice when we do that it starts at the first one and it runs all the way through and then they all have that because we are delaying the animation what's interesting with animation delays is if you have it as a negative number it's not actually it's going to just start where it would have started right they the delay we're not delaying 100 milliseconds before the next one starts it's just saying like if there was a delay but we were starting in the middle of the animation where would we be so it's in this case probably nicer actually without the animation delay on there but with sometimes if depending on the situation that you're in sometimes you need them all just to be starting at the right spot rather than you know having that slow delay that would happen every time you start something really depends on the use case it's not as often you will need the negative on there but every now and then you'll be very happy you know about it and this next one is less so of an animation tip well it's very important for animations and motion in general that I should say but what we generally or what we often want to do is be aware that people don't always like motion and they have preferences in their browser they can turn the motion off and then they get to this website that might be attacking their senses and it can go really far into the point of causing nausea or headaches and they're going to leave your website if they're not enjoying the experience but luckily it's really easy to accommodate all we have to do is we can come here and we could say at media and we use a prefers reduced motion no preference and I'll make that a little bit bigger so we can see it all and just wrap it like any old media query uh right so we just have something like that and now for me where I have no preference set my animation is still running but if I come and I inspect on that and in your inspector you can do are in Chrome anyway you can do a control shift p mac would be a command shift p and I can do an emulate and prefers reduced motion reduce which is the system setting and look the animation is not running for some things you might want to keep the animation if it's little things it's not turn off animation it's not no animation or no motion it's prefers reduced motion so you could use this to reduce the motion that's on things but keep little things around there like you have a toggle button that slides across that's not bothering anyone it's more for things where you have vestibular orders or other stuff where motion that's moving in the wrong direction so as you're scrolling and then you have things that are going in a different way you could always disable that just by wrapping everything inside your no preference right there and this could even be for your smooth scrolling as well when you click in it smooth scrolls down some people really throws them off my wife hates pages scrolling so yeah for those types of things it's really easy to do and we just want to be aware that well you might really like that motion you've created and it looks amazing there are a lot of people out there who not only don't like it but it actually can have physical effects on them so we want to be careful about that and if you'd like to know more about this because i'm just sort of brushing over it really quickly right now but i'll link to another video in the description where I did a bit more of a deep dive on this and now before we jump into the next one i just want to say that this is far from an exhaustive list so if there's any tips or tricks or anything like that that i've missed along the way leave a comment down below and let let me know and let everyone else know because there's animations there's so much cool stuff we can do with them but let's jump into the next tip now now for the next thing that's interesting is not only can we do multiple ones like this but i actually learned something along the way in that challenge that i talked about with amit before that didn't work for the thing but it made me learn something new let's go back up to my button and on this one what we're going to do is on the party here we're going to add something that's maybe a bit weird but let's do a rotate of 180 degrees and so now when i click it it's going to spin around and spin back around now this is also gone a little bit crazy in the background because the party is being used on those other guys and i wasn't expecting that i think it looks kind of fun but it's a little bit distracting so let's take that off and you know what we're just gonna take this whole thing off we're not going to worry about the buttons we're just going to worry about that guy that's rotating 180 degrees back and forth and what we're going to do is let's say it's going to take a thousand milliseconds to do it so i click and it's going to go there and come back and it only does it once and then i can actually do we can have multiple animations we can actually do a comma and do party again and this time we're going to do it over 2000 milliseconds these are both going to run at the same time so it's a bit weird so let's come here and do a 1500 millisecond delay as well so i can click it's going to do the first one and then the second one and then you could do a third one if you wanted to so just a comma and then we can do party let's do 5000 milliseconds and we'll have to have a 1000 2000 we'll say a 4000 millisecond delay on this we're getting really exaggerated and let's do this one as infinite infinite alternate just because why not i don't know if i spelled that right we'll find out in a second so i click first one second one and third one much slower and that one should keep running as long as i spelled everything correctly which it looks like i did so just to say in this situation again silly demo probably not the best use case for it but it is interesting that you can actually you know you could have the animation run once in one way and then run again in a different way or to continue running once it slowed down or whatever it is you could come up with some interesting stuff here all right and now the last tip is an interesting one it's something that i've talked a little bit about and people have been asking me to talk more about which is or let's come we're going to look at our button here let's turn off that translate and let's change this background a little bit that's on here i'm going to set this as a linear gradient linear gradient gradient from we'll do red to blue to match my background and hit save let's do 45 degrees here or 90 degrees i guess we'll just go left or right 90 degrees there we go have that set up and yeah so we have that and people often want to animate the gradient that's on there there's tricks of doing it where you're sort of moving your gradient around but one thing we can't do is animate this red color here and so just to show you if we come here and we change this and let's make this blue and we'll make this one go to red on that side and this will be my background and background is not an animatable or a background image i should say is not an animatable property so when we have that you can see it doesn't animate and if you're saying it's because it's not the same property you can fix that right there it still doesn't animate or transition because it's not an animatable property because it's looking for an image basically and we can't really change the value of red and so the first solution that people got very excited about let's just come here and say this is uh color one will be red and then color two will be blue and then give me one second and i'll get all of these set up and there we go we have it set up and you can see it's still working i still have it and then when we could come down here instead of updating this you know you'd think it would make sense that we could update these two values right there instead but we still get the same thing and that's because custom properties are something that could literally be anything the browser doesn't have enough information to know if it's actually able to do anything with them there's a lot of technical reasons why we can't animate a custom property in the way i'm trying to do right here and this is happening because once again we're trying to you know transition the background image and luckily recently or chromes had this for a while and very recently it just got added to safari so we're just waiting on firefox but we have at property and at property allows us to declare or to register custom properties so i'm going to have at property and we're going to say color one and we have to give it three different things we want to give it a syntax inherits and an initial value so the initial value we know we want this to be red at the beginning the inherits could be true or false it's up to you i'm just going to say it's true is why not and the syntax here is going to be a color and you can see vs codes syntax or they're they're highlighting here is not too happy with me but that's fine i'm going to take these as far as the syntax it can be a lot of different things but since we're using colors and it doesn't have to be a keyword it could be any color hsl rgb whatever so color two will be blue and everything broke for me because i forgot to put this inside of quotation marks there we go and look the syntax highlighting is better too i think though it could be a little bit prettier there we go gotta hit the right keys now it's working and now the advantage of doing this and the advantage of registering a custom property is if and just really fast like you just look up the different things you need colors numbers different values strings there's a whole bunch of stuff so now instead of transitioning a background image which can't be transitioned we could transition my color one and we can just do a comma here and do my color two over 1000 milliseconds so now if we hover on top oh we can see it's not going that way but it is going the other way that's because this transition is getting in the way so let's hit save and now you can see that it's transitioning the colors it's not transitioning the image it's transitioning the custom properties themselves and this is kind of cool because you could actually do like different types of things right so it completely changes the style maybe this one even gets like a 1000 millisecond delay on it so it's all the whole thing goes blue and then the red comes in there's so much fun stuff with this i'm so excited for app property it's part of houdini there's lots of really other awesome stuff coming with it as well but the control we'll get over our custom properties with this is going to be amazing and as i said it's already supported in chrome it's very recently landed in safari and it's really really cool what we can do with it and as a reminder if you want that video where i talk more about prefers reduced motion as well as two other very simple changes you can make to your css they can make very big improvements on it that video is right here for your viewing pleasure and with that i would like to thank michael simon tim and johnny who are my supporters of awesome over on patreon as well as all my other patrons for their monthly support and of course until next time don't forget to make your corner the internet just a little bit more awesome