Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
erayner_45092

How to manipulate flex values?

Recommended Posts

Hi there!

 

I am just looking into ways to animate things without using position absolute and left and right properties.

with flex I can center something in the middle of the page very easily. How would I go about animating something from right to center but not using position absolute?

 

Cheers :)

Link to post
Share on other sites

You can't animate most flexbox values because they're words, e.g. flex-start, space-around, column-reverse. You can't say, animate to column reverse. The browser has to do the layout. But that's actually a good thing as that's one less thing you have to calculate.

 

To do a flexbox animations, start off by recording the position of your element in it's current state. Now change its flexbox style and let the browser reposition it. Now record the new position of your element. You now know where the element was, and where its supposed to be. Now move your element back to it's old position and animate it to it's new position. This all takes place in between animation frames, so you won't see the jump.

 

See the Pen zwWgao?editors=0010 by osublake (@osublake) on CodePen

 

This technique will work for every flexbox property.

See the Pen dMLQJr?editors=0010 by osublake (@osublake) on CodePen

 

It will actually work for any type of layout that the browser handles, like the new CSS grid.

 

For more information, check out these threads.

 

  • Like 14
  • Thanks 1
Link to post
Share on other sites

Great stuff, Blake. Super cool.

  • Like 2
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×