This course has been updated! We now recommend you take the CSS Animations and Transitions course.

Check out a free preview of the full Motion Design with CSS course:
The "Exercise 6 Solution" Lesson is part of the full, Motion Design with CSS course featured in this preview video. Here's what you'd learn in this lesson:

Rachel walks through the solution to exercise 6.

Get Unlimited Access Now

Transcript from the "Exercise 6 Solution" Lesson

>> [MUSIC]

>> Rachel Nabors: The nice thing about skrollr is that it doesn't require you to write any JavaScript or any CSS. Once you have it on the page, you set up the values that change using data attributes. So the same that data attributes that work for Alice should also work for Tuna.

[00:00:22] But there is one trick. Those values that you were interpolating, remember that the zero has a percent sign next to it. This is just a little quirk of skrollr. The properties need to have the same kind of values. So it's not trying to interpolate between picks and M so top should be 0%, bottom should be 0%, not 0, like you can write in CSS.

[00:00:48] Remember to put that percentage sign next to the 0. So I noticed a lot of people got the skrollr to work. This is a fun project because you get to work with both stateful animations, her change in her attitude but you also get to work with this dynamic animation tied to the scroll position.

[00:01:14] I say dynamic because it is interpolating actively where it should be. So the first part of this challenge and the most important part was that we need to get Tuna to flow from the bottom to the top.
>> Rachel Nabors: Guys, all right. So the first step is we're gonna take this data attribute that we have on Alice, data 0 and data 6124, and we're gonna put them on Tuna, right?

[00:01:48] So now, Tuna has, he's gonna move in the same way Alice does, but this is not what I described. I described Tuna moving from the bottom to the top in opposite direction of Alice. So let's try changing those top to bottom. Now we've got him moving in the same direction.

[00:02:10] But he doesn't move fully off the page. And I want him to be fully off the page. So let's try negative 200%. And we're gonna go bottom 100%, this will get him fully off, here we go. So now he's there. Make it 120. And now that's working, and tunas values are now automatically updating via JavaScript, writing inline styles as he moves up and down to confirm.

[00:02:39] Let's inspect his element, and we can see, if this thing gets out of the way, Squish, so much tabbing and indenting Chrome. So you can see that the purple parts are updating both for Alice and for Tuna. But there's also something interesting you may have noticed about Tuna.

[00:03:01] I gave you a challenge to turn him into the Cheshire cat. If you inspected his element so you looked at his HTML, you'd see that there are some images inside him. Some very damning images. If you didn't know better, it almost looks if you took one of them and got rid of it, he would be a completely different cat.

[00:03:24] That would be this one. If I said display none on that one, and then I did opacity, say this one has an opacity of 0. If I did opacity 0 and a 1 on the red one, suddenly Tuna has an expression so there's actually three images sandwiched there.

[00:03:44] Some of them are hidden and some of them aren't. So we're going to want to change Tuna's appearance based on those things. Now we have an option here, here's where the road splits. You could use the dynamic animation, you could take the same data attributes that we used on Tuna.

[00:04:10] Let's see. Grab these and we could say well, we wanna show Tuna. We wanna have the real cat, the Tuna glow changed in a different rate from the black-tuna, so we would put these both here. And we would change the opacity from, let's see, I might be doing this wrong.

[00:04:37] Everybody feel free to correct me if I am.
>> Rachel Nabors: So notice that now as we move up, he fades in. Woo, so cool, so awesome, right? Nice. Love you Tuna, you're awesome. And we could change when that happens. So that maybe it happens closer to the middle of the page.

[00:05:03] Instead of happening at the beginning, we could change. Let's say we start at 400 from the top. And end it when it gets, so notice we've delete it here by changing those data attribute values. And it's fully finished before it reaches the top. So that's one way we could have done it, but there's another way, the stateful way.

[00:05:25] You may have noticed that as Alice is mood is changing,
>> Rachel Nabors: [SOUND] As she's changing, the body class changes as well. She goes curious, bored, sleeping, waking, etc.. Well, the body class, it can scope any of its contained elements to that. So Tuna could technically change his appearance when the body has a class of bored by scooping Tuna with that bored class.

[00:05:55] Let's have a look at that. So let's first figure out exactly where we'd like him to change into the Cheshire cat. I would say that would be right about, let's see, she's here, she's looking around, just getting a little bored. And then she wakes up, I would say maybe right about there.

[00:06:13] So this is where she wakes up. That would be, here's where I go find that particular name for that thing. There we go, bored, sleeping, waking, waking Alice. So around that time, let's see, was Tuna glow so waking tuna-glow and it should be opacity 1. So right around the time that she wakes up boom.

[00:06:46] But you noticed it happened all at once. Right? We could slow it down by overwriting its default transitions, I already added the transitions to Tuna cuz I was totally hoping you catch the Easter egg but I'm gonna override some of that. I'm gonna say transition-duration should be 6 seconds here.

[00:07:05] So now, when we scroll, and she's waking up, he takes 6 seconds. But notice that when you do stateful animation like that, it takes 6 seconds. It's not tied to how fast the person is scrolling. It just happens or it doesn't happen and I would probably also define that.

[00:07:26] While she's awake, he would also remain opacity 1, etc.. You have to repeat yourself sometimes.
>> Speaker 2: I've got a question from online.
>> Rachel Nabors: Yes.
>> Speaker 2: The question is from Kevin L.. We would like to know what would be the cons of attempting to use skrollrs, viewport-anchors to interpolate Tuna's position?

>> Rachel Nabors: I don't think there would be any cons. Skrollrs a pretty cool library, it has a lot of options. I don't do a lot with parallax so I can't speak definitively on that. But it has a lot of handy functions for figuring out where things are located inside it and sometimes even recalibrating where things have moved to.

[00:08:09] So if you're interested in using skrollr, I highly recommend checking out skit hub pages. There are links in the show notes as it were and seeing what features are available to you there.