Web Development

GSAP Flip: Animating Layout Changes Without the Math

Team Nippysoft
• • 7 min read
GSAP Flip: Animating Layout Changes Without the Math

Flip is the GSAP plugin that animates layout changes: switching a gallery from grid to list, filtering it or moving a card to another container. That change is one line of CSS or DOM code and the elements jump into place; Flip measures where each one was and animates the difference, even when the DOM structure changes. It has been free since 3.13.

An online store's catalog uses all of it: the grid turns into a list, a color filter narrows it down, and products marked with a star fly over to favorites. That is the demo in this post.

This post is part of the GSAP series. Licensing and installation live in the series hub, and stagger, matchMedia and context in post 2, on the core. It includes one live demo on GSAP 3.15.0. Two script tags at the end of the <body> are all it takes:

<script src="https://unpkg.com/gsap@3.15.0/dist/gsap.min.js"></script>
<script src="https://unpkg.com/gsap@3.15.0/dist/Flip.min.js"></script>
<script>
  gsap.registerPlugin(Flip);
</script>

At the end of the <body>, because Flip sets itself up with document.body: with everything in the <head>, the first getState() throws a TypeError ("I is not a function"). Having the registerPlugin call in the <body> is enough. In the snippets, .gallery is a container with display: grid and each .card is one of its children.

FLIP in four steps

FLIP is an animation technique: the letters stand for "First", "Last", "Invert" and "Play", and the Flip docs credit the name to Paul Lewis.

  1. First: record the starting state.
  2. Last: put the elements in their final state.
  3. Invert: instantly apply offsets so they look like they never moved.
  4. Play: animate those offsets away.

The browser has already laid out the final state; the animation only undoes a difference.

The recipe: getState, the change, from

const gallery = document.querySelector(".gallery");
const state = Flip.getState(".card");
gallery.classList.toggle("list");
Flip.from(state, { duration: 0.6, ease: "power1.inOut", stagger: 0.04 });

getState() only takes notes: "Flip.getState() doesn't alter anything", except that it completes a flip already running on those targets so it can measure them. It always records transforms, width, height and opacity; props: "backgroundColor,color" records those too. And Flip.from() "returns a timeline", so it can be paused, reversed or nested like any other (timelines are in post 3), with any ease (post 4).

Case 1: grid to list

That snippet is case 1: a class takes grid-template-columns from three columns to one. The CSS:

.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.gallery.list { grid-template-columns: 1fr; }

Case 2: filtering

To filter, a class with display: none hides the cards that do not match. The recipe alone falls short here: leaving cards have no end state and entering ones had no start state, so Flip does not animate them. It hands them to onLeave and onEnter instead, and any animation the callback returns joins the flip timeline. Leaving cards also "won't be visible unless you also set absolute: true": they stay on screen while they leave, and without position: absolute they would push the others around. absoluteOnLeave: true applies that to the leaving cards only, and it is what the demo uses:

const state = Flip.getState(".card");
document.querySelectorAll(".card").forEach((c) => {
  c.classList.toggle("hidden", c.dataset.cat !== "blue");
});
Flip.from(state, {
  duration: 0.6,
  ease: "power1.inOut",
  absoluteOnLeave: true,
  onEnter: (els) => gsap.fromTo(els, { opacity: 0, scale: 0.8 }, { opacity: 1, scale: 1 }),
  onLeave: (els) => gsap.to(els, { opacity: 0, scale: 0.8 })
});

It assumes .hidden { display: none; } and a data-cat on every card: without it, the filter hides them all. With visibility: hidden instead of display: none, Flip never calls onLeave and the gap stays taken.

Case 3: moving to another container

Moving a node to a new parent with appendChild is just another DOM change, so the recipe stays the same. When the new parent is rotated, Flip "just works", in the docs' words; when it is scaled, the height jumps on the first frame unless scale: true is set. zIndex lifts the flying cards above the rest of the page:

const card = document.querySelector(".card");
const favorites = document.querySelector(".favorites");
const state = Flip.getState(".card");
favorites.appendChild(card);
Flip.from(state, { duration: 0.6, ease: "power1.inOut", zIndex: 5 });

zIndex only takes effect when the card ends up positioned or its new parent is flex or grid. In a block container, a positioned neighbor with z-index: 3 covers it; absolute: true prevents that. In the demo, favorites is a flex container and the star does the move, without stagger.

The demo: a gallery

The demo puts the three cases together in six cards, and nothing moves on its own. Under reduced motion the buttons still animate, without stagger and in 0.4 s, through gsap.matchMedia(), as covered in post 2.

1blue
2green
3blue
4green
5blue
6green

Favorites

Showing 6 of 6

Options that change the result

OptionWhat it does
absoluteApplies position: absolute during the flip, which prevents jumps in flex and grid. Because it takes the elements out of the flow, content below can collapse; in that case it is better applied to some of the elements only, leaving the container out.
scaleScales instead of changing width and height, which typically performs better.
nestedWhen a parent and its child are both targets, stops the child from moving too far.
zIndexSets a z-index for the whole flip and reverts it at the end.
toggleClassAdds a class while the flip runs.
pruneLeaves out targets that do not change, so the stagger spreads only across the ones that move.
spinAdds an extra 360-degree spin.

Two elements, one data-flip-id

Flip pairs elements through their data-flip-id attribute. When two different elements share one, such as a thumbnail and its full-size version, Flip works out which one disappears and swaps between them; fade: true turns that into a crossfade:

<div class="thumb" data-flip-id="photo"></div>
<div class="full hidden" data-flip-id="photo"></div>

<script>
  const thumb = document.querySelector(".thumb");
  const full = document.querySelector(".full");
  const state = Flip.getState([thumb, full]);
  thumb.classList.add("hidden");
  full.classList.remove("hidden");
  Flip.from(state, { duration: 0.6, fade: true, absolute: true });
</script>

Both must go into getState(): with only the thumbnail, or with different data-flip-id values, the full-size version shows up at its final size with no transition.

When the browser is enough

The View Transition API is the browser's own take on the same idea: document.startViewTransition() takes the DOM change and the browser animates from the old state to the new one. According to MDN's compatibility data, it ships in Chrome and Edge 111, Safari 18 and Firefox 144. What Flip adds, per its docs: the result is a GSAP timeline that can be paused or reversed, with ease and stagger; per-element onEnter and onLeave callbacks; and options such as nested, absolute or spin.

Common mistakes

  1. Calling getState() after the change. With the DOM already changed there is no "before" left to animate from, and the elements jump into place.
  2. Transforms in CSS classes. GSAP caches transforms, so it misses the ones a class changes. The docs ask for setting them through GSAP, or clearing that cache with gsap.set(el, { clearProps: "transform" }).
  3. Expecting 3D transforms. "Flip does not accommodate 3D transforms (like rotationX, rotationY, or z)."
  4. Scrolling or resizing during a flip with absolute. Coordinates come from the viewport, so if it changes mid-flip the positions drift until the flip ends.
  5. Frameworks that re-render. Flip.from() has to wait for the render, and if the framework creates new elements, they go in as targets with their data-flip-id.

FAQ

Flip or View Transitions?

For a crossfade or a simple swap between two views, the browser API. For a timeline that can be controlled, per-element callbacks or options like nested, Flip.

Why don't disappearing elements animate on their own?

With display: none they have no end state to compare against. Flip passes them to onLeave, and absolute or absoluteOnLeave keeps them visible on the way out.

Conclusion

The basics live in the series hub, post 2, post 3 and post 4, with scrolling and text in post 5 and post 6. Next up is post 8, on Draggable and Inertia: drag, throw and snap; and in post 13, the final tutorial, the landing page gallery runs on Flip.

References

Subscribe

Get the latest posts delivered right to your inbox.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Comments

No comments yet. Be the first to share your thoughts!

Subscribed!

Registered! A confirmation link has been sent to your email address. If you don't see it, please check your spam folder.

Error

An error occurred. Please try again.

Full code