Syntax Video
We Tried Recreating This Beautiful Card Animation
Transcript
Okay, so we're trying to recreate these social cards from Lando's site. Wes recently did a breakdown of all the stuff on the site, and we actually recreated this menu here, but today we're jumping in recreating this. So, as you hover, the other cards kind of fan out. They also have like this this angle. They're kind of like rotated to them. It's like holding a hand of cards.
And we have a some base HTML. So, we have all the images on the page here. We got to make it work like Lando's website. All right. trying to recreate this Lando Norris hover effect. We're going to go ahead and code that up. I'm hoping I can do it entirely in CSS. I'm going to first just get the cards in place. So, the cards is going to be display grid.
Let's do that. And then grid template columns. I'm going to just hardcode the number of cards for now. The HTML here has cards in it, but I don't even know if we need those cards. You could do it with just these images. In fact, I'm going to keep it simple and I'm going to nuke this CSS or this HTML. Why why am I doing this? This is not the the smartest thing to do. Already regretting this choice of removing these divs, but hey, I don't care. So, let's target the card.
Let's do a width of 30 Vmin and a height of 50 Vmin. And I'm going to use Vmin so that way it automatically resizes with the page width. And we'll give it a border radius. For now, we'll just do like 10 pixels and then we'll dial it in. Do I want to use CSS grid for this or do I just want to put them all on top of each other? Maybe I want to. Maybe I do.
Let's see. So, we're going to say we have the cards.cards. And inside of our cards, this is going to be a position relative container because we're going to want these cards, individual cards inside of here to be positioned absolutely. And we're going to want to do it kind of in an interesting way. Now, this is kind of similar to the circle example that we did. And I'm not sure if the guys are realizing that we have each of these cards. Fantastic. Now, let's get the image centered inside of them. So, the image inside the cards should have uh full width and height. And then we can do object fit cover. So, that should make it centered inside. Yeah, like that. From there, let's make the parent a flexbox. Like that. Cool. And then I don't want these boxes to shrink because right now if you do this they shrink in size but I want them to always be the same size. So on the card itself we can do flex shrink zero and then they always
stay the same size. Fantastic. Let's make every single one slightly smaller so they overlap each other. Yeah. Okay. I like that. Border radius 10px. And then we want to center everything. So justify judge duty justify content center. Beautiful. All right, I think that's that's good enough for now.
Um, so we're going to say that these have a position absolute. They're going to be a width of like something like 270 pixels for right now. And they're going to have a big old border radius. Border radius. Oh my god, I am regretting removing those divs. Oh my word. But now, let's get this one positioned underneath the one in the middle. Cuz the one in the middle is always going to say in the same spot except for on hover, but then the others are going to be like scrunched in. So the one to the left I want to change its transform X. I think I can just do in child. Like I'm not I'm going to stick to CSS on this one. I don't want to use JavaScript like I have in the in past challenges. I'm going to challenge myself and try to just use CSS. So I think if I do like select each of these images directly and then set their transform directly.
Now we need to fan them out like that. So every single basically we got to put a rotate origin like right here and then they all need to angle from there. So what I'm going to do is each card is going to have a rotate value which is going to be something like let's just put a 45Β° on it.
The height 100% is the problem but like we want the height to be 100%. It is a position absolute inside of a position absolute div. I want to do um an arc clip path, an offset path like I did for the other one. And it's really just a handful of things. I do have an arc path generator thing here loaded up. And so this is going to be using offset path and each of these divs will be on an offset path.
I think we're pretty good here. That's decent. So let's just use these values and then kind of extrapolate for all of the the surrounding ones. So, the second child should have a z-index of two. And then we want to move it. Let's see if we use the same translate. No, we want this one to be more uh translated to the x cuz it needs to be underneath the one in front of it more. So, what if we double it? It's pretty close. And then what if we double the y? That's pretty close.
So, yeah, that's looking decent. So, now what I want to do is every single card needs like a default value. Oh, the the origin needs to be based on something else. How do you transform origin based on the parent? That's not really the type of arc I wanted. I was trying to copy it off of that candy cane one. Oh man.
No, that doesn't look good. Um I We're already halfway through. Only 10 minutes left. I need to get this right cuz it's not going to look good if they're not rotated right. So the one before it should be rotated more. So let's go -10. And then that one should be rotated maybe five more than that. And then that one should be rotated maybe five more than that. And then we'll adjust all of the the Y based on that. Yeah, like this. This is this looks better, I think. I mean, once we adjust the Y.
I suppose it is kind of like a rainbow here. I could just do my circle one. I'm going to do my circle. Sorry, folks. I'm going to copy and paste this directly from our old project. It's a circle. It looks like this. Dear God, I'm I'm in trouble. Beautiful. Okay. Way better. That's way better. Okay, I'm going to give each one a X and a Y walk. So, we'll give it an X of zero and a Y of zero. And then for each card, we will increase the walk.
I think this is perfectly fine. Okay, so now let's make it so that when you hover it increases in the X because it goes, you see that it shoots up. So, let's get that working first. So when you hover over any card, we want to increase the translate X by a certain amount. I don't know what to do for this offset path other than just bump up the values by a whole lot, which is like really not flexible at all. And honestly, I don't love that as a solution. Okay, offset distance is 49. Yes, this is too big of an arc, but it is an arc. Finally, there we go. Bottom right. That's what we wanted. Okay, now we need a way to programmatically change this, right?
There's still they're still stacked way too much. 200 is way too much, man. This needs JavaScript. You can do this in CSS, but kill me. What did I do here? Let's move the transform to the card selector. So, we have all of these variables and then we use them. So, we only have 6 minutes left. I might only get the like hover move up animation.
Uh, offset position. Is it offset position or did I already goof that up, too? Did I change what that was? Oh my god. I have to look at my old code pen. It's offset distance. Offset distance, not offset position. You want to see somebody whose brain is not functioning today? That is me.
Why are they off? Panicking. Oh, it's cuz I'm doing bottom right, bottom center. Oh my gosh. Let's see. It works. Okay, so now when you hover, they go down. Uh, we need them to go up. So, we'll do negative -2 of even. Yeah, look at that. So, now when you hover, they go up. Let's also add a transition uh duration. So, transition, we'll do half a second. We'll do all half a second linear. Nice. We got to do it faster though. So, 0.25.
Even faster. So, zero is the initial value. So, if we were to even say like 200 is the initial value there. So, we could bring this all in with just the SVG path if we wanted to. You could also just do this with margin, which it's probably not a bad idea. Margin, we'll say 100 pixels. And then let's say 200 pixels. Squish this in here a little bit. And even let's do instead of that, we'll just do auto.
Um, let's take the card and put a transition on it. It's got to be super fast. We need a custom bezier curve on that as well. Does it scale up as well? I'm going to put my fingers on the screen. No, it does not scale up. It just goes up. That's not bad. Okay, so 3 minutes left. How are we going to get these things to fan out? I think again we can like select them manually. So when we hover over one, we could say everything to the right, we increase the X, so it pushes out more. And then everything things to the left, we pushes out more.
So for each element, select all the elements to the right, select all the elements to the left, and increase their translate X. It'll increase more as it goes out. Even that little stupid rotate that I did is wrong. And now I'm changing the percentage values. Oh my god. Which one is this? Are there seven images? If there's seven images, I'm going to 1 2 3 4 5 6 7. Oh, I did all this math. Four, six images. That's great. You know what? I should have done it with actual math instead of whatever the hell I'm doing. The x needs to be incremented by however much it's defaulted to plus more. What we need to do is we need to take all these x values and put them in variables and we'll calculate the default add 40. Oh yeah.
Okay. That doesn't look like it at all. Two minutes left. It's not very good at all. I got this kind of like hard fan. That's great. If you hover over any of them, we're going to move them up a little bit. We're not going to move them over. We're going to move them up just by like I can't even decide. 10 pixels.
That's not enough. That's not bad. And it's always the one to the right. Look at that. Okay, that's not bad. Um, but then we can do the same thing for each one after that. So, do the one to the right. The one to the right after that will be uh plus card plus card. Increase that just a little bit less. And then the one after that.
We only have a minute left. This probably isn't the best way to do that, but this will at least when I hover over the middle, they Yeah, look at that. They all They all move. It's pretty good. This is a tricky one right here. So, when a card has a direct sibling that is percent, which is a card being hovered, then take that and subtract 40.
So, boom, boom, boom. Oh, boy. Oh, man. We're getting it now. I know it's a bouncy transition. That's not fast enough. I I'm going to actually quickly grab a CSS linear function uh for bouncing. Okay, I see all the math. Just give me the linear code. Okay, how'd you guys do? Uggh. Bad. Not great. Okay, I need a lot more time.
Yeah, I'm still working. My brain is Yeah, crazy. All right, everybody thought we could do this in 20 minutes. We can't do this in 20 minutes. I think what I need to focus on right now is every single one has a slightly different animation. So, we got to figure out what all of those things are. I guess what I was thinking isn't exactly right. Like, cuz when you when you hover over this one, everything to the right of it should move.
Oh, okay. So, it turns out I suck equally to everyone else. And it's not that I suck more. Yeah. I don't know, man. Okay. So, this isn't bad. I think the main issue is getting it to limit how far it moves out. Cuz like these last couple, we need to make sure that they don't go. It's really only those last two. I think these always move in the same amount regardless.
The cards move the same amount no matter which one. You're right. Okay, I'm just going to put my finger on the screen. So, that's where it goes when you hover the middle one. Okay, the cards go to the same spot every time you hover them, no matter what. Every card just has a a spot where it goes. Oh my gosh, this is so confusing.
I'm very happy with this. This is fine. This is perfectly fine. Happy with it. It's really not terrible. It's not terrible. It's really not like mine's just a lot of code. It's kind of hacky in that uh I only use CSS, so which is which is a first for me. Um Oh, wow. You didn't install entire framework.
Exactly. So, it's it's not pixel perfect, but Oh, yeah. pretty happy with this. Basically, I'm using sibling selectors. So, yeah, uh for any given element, select the six elements to the right and they change their offset X. This is what I was talking about. There's a lot of CSS because I have target the first card, target the second card, target the third card.
Oh, use max. Okay. The ones on the edges don't move very much, which is why I'm using that max. So, that's what I that's one of the last things I plugged in. I couldn't figure out how to get it working on the main selector. The last two on the left and the last two on the right, I'm using a max so that they don't move too much. But uh one thing I had to look up, you can see all of my sibling selectors are pretty crazy. So So the So if you're nuts, yeah, if you're hovering over a card, select the card after it and then select two cards after it and then three cards and four cards. So that that increases.
But then for cards before it, I had to use the has selector. So select the card that has a card hovering it to the right or to the Yeah, cuz then cuz that and that selects it to the left. Yeah. Yeah. So I got pretty good. Beautiful. Thank you. Good job. I'm proud of you for not installing view.
I'm trying my best. My implementation I it feels pretty good. And what I figured out is that every single card has two states or three states currently being hovered. Mhm. A card before it being hovered and a card ahead of it being hovered. So what I did is I went into every single card and I added a default X value.
Mhm. And a behind and ahead increment meaning that this is how much it will move plus its current spot when it is being hovered. So then my code here is much shorter than CJ's. I say when a card is being hovered, change its Y value by -40 pixels. Right.
So every time you hover one, it goes up by 40 pixels. Yeah. Then what we do is we take all future cards of the currently hovered one, change their X value from its default value, and then subtract the amount that card should uh move X or Y. And I probably could have got away with one v variable here now that I look at it cuz they're all exactly the same, just negative and positive.
And then I use I say when a card has something in front of it that is currently being hovered. So basically this is selecting all of the items before the currently hovered card. Then I'm applying the ahead value here. That's great.
Yeah. And then I used a custom bezier curve. It doesn't feel as good. I really need to spend some time getting the bezier curve better, but I did kind of Yeah. And I think yours need to rotate when they move too, right? Like when you hover over like number three, like number four isn't rotating at all. It's just moving to the right.
I don't have that. But if you look at the original animation, there's it's like a tiny like one or two degrees extra. Yeah, that's my Are they not? I'll show you I'll show you the solution for that because uh I need to see I think combining there are some things that I missed in mine uh that make it so mine isn't perfect but I think my the path that I went down pun intended was in fact the right solution. So I chose to do a very similar technique that I did on the circular menu thing and it uses offset path. So offset path is great because when you hover over one, all you're doing is you're just telling it move over a percentage and it moves over on the same rotation thing. The way that I did this is there's some neat things in this implementation cuz this is it for CSS for me. I didn't write very much which is nice, right? There wasn't a whole ton of extra stuff. The crux is this delta offset. So on hover,
I'm giving the item next to it an offset of an additional 8%. And I just calculate that into the offset distance. And the offset distance is just from 0 to 100%. And then also I'll Oh, this should be 8%. Butgative8% using the has for the item before it.
And then that will travel them along. The things I didn't get done are a couple. The zindex, I don't have z-index set correctly on these. So you can see that they're just stacking on top of each other. And then also in their example, something that uh none of us were able to do was that the items on the edge are like scaled down slightly and then as they get to the center they're scaled up slightly. So like I actually was like googling some math for that. It was like giving me actual equations. So I was like, "Oh no, I can't do this in time." to like clamp zero to 100 to 80 to 100 to 80% or something.
Yeah, you you basically need to like calculate the value for all of them and then offset it by half of that amount, right? That's like kind of like you want a random number between X and Y. It's the same same thing. I will say my my jumbliness feels really good. Yeah. And the reason why is I did use linear for my easing curves. This is from a easing curve generator and I was finding that it's like the bounce style linear curves uh were too weird looking and then I found one that was a spring style linear curve and that's why these they feel so bouncy and nice. Yeah.
Such a benefit to adding these the linear ones. All right, that was fun. Let us know down below in the comments who you think did the best and who you think did the worst. Uh very frustrating. Keep in mind we're stressed out. We're doing this. running out of time. Um, but let us know if you have any more UI things that we should rebuild or figure out how how it is. So, anytime you see something neat on a website, send it our way. We love to build it.
Quote it with credit and a link. Quoting Syntax