Create Dynamic Animation Values with Framer Motion for Different Elements with Custom

Will Johnson
InstructorWill Johnson
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

The custom prop let's you create dynamic variant values to animate each component differently.

.

Instructor: [0:01] Here, we have a list of four items. Let's see what happens when we refresh the page. Each item animates in all at the same time. While that's OK, I think that we can do something better. Let's make it to where each item animates one right after the other.

[0:21] Framer Motion makes this really easy when you're using variants because, when you use variants, every child component is able to be animated. However, since I did use this exit prop, staggerChildren isn't going to work as expected.

[0:42] Now, what we can do inside of our variant is use custom data that's dynamic. Let's go to the visible variant label. I'm going to paste some new information in here. Basically, I pasted in a function that returns an object. The opacity, when it's visible, is still going to be one. That doesn't change, but the transition now is going to have a delay.

[1:14] Since we want each of these items to animate one at a time, the delay is going to be a dynamic value. That's where custom comes in. Let's go down to our list item, and let's add a custom prop. I pasted in index plus one. We'll go through each item that we have listed, and they'll be multiplied by .2 seconds. Every time we add a new item to the index, it'll add .2 to it.

[1:51] The expression that we pasted in will be the value of the transition delay that we have. Now, when each addItem() is animating onto the DOM, each item in our list will animate .2 seconds behind the other one to become visible. Let's save and refresh the page.

[2:17] As you see, each item waterfalled in right after each other. In order to do that, we had to make an arrow function, pass in custom, have it return an object with a dynamic value for custom, then go down to our list item element. Then, every time that we add something to the index, we want to times it by .2 seconds, and that will become the value of delay.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today