Flip.from
Flip.from( state:FlipState, vars:Object ) : Timeline
Immediately moves/resizes the targets to match the provided state object, and then animates backwards to remove those offsets to end up at the current state. By default, width and height properties are used for the resizing, but you can set scale: true to scale instead (transform). It returns a timeline animation, so you can control it or add() other animations.
Parameters
state: FlipState
A state obtained from Flip.getState().
vars: Object
The vars to use for the from animation. You can use any standard tween special property like
ease,duration,onComplete, etc.
Returns : Timeline
A timeline animation
Details
Immediately moves/resizes the targets to match the provided state object, and then animates backwards to remove those offsets to end up at the current state. By default, width and height properties are used for the resizing, but you can set scale: true to scale instead (transform). It returns a timeline animation, so you can control it or add() other animations.
Usage
There are typically 3 steps to a "FLIP" animation:
-
Get the current state
// returns a state object containing data about the elements' current position/size/rotation in the viewport
const state = Flip.getState(".targets");This merely captures some data about the current state. Use selector text, an Element, an Array of Elements, or NodeList. Flip.getState() doesn't alter anything (unless there's an active flip animation affecting any of the targets in which case it will force it to completion to capture the final state accurately). By default, Flip only concerns itself with position, size, rotation, and skew. If you want your Flip animations to affect other CSS properties, you can define a configuration object with a comma-delimited list of
props, like:// record some extra properties (optional)
const state = Flip.getState(".targets", { props: "backgroundColor,color" }); -
Make your state changes
Perform DOM edits, styling updates, add/remove classes, or whatever is necessary to get things in their final state. There's no need to do that through the plugin (unless you're batching). For example, we'll toggle a class:
// make state changes. We'll toggle a class, for example:
element.classList.toggle("full-screen"); -
Call
Flip.from(state, options)Flip will look at the
stateobject, compare the recorded positions/sizes to the current ones, immediately reposition/resize them to appear where they were in that previous state, and then animate the removal of those offsets. You can specify almost any standard tween special properties likeduration,ease,onComplete, etc. Flip.from() returns a timeline that you canadd()things to or control in any way:// animate from the previous state to the current one:
Flip.from(state, {
duration: 1,
ease: "power1.inOut",
absolute: true,
onComplete: myFunc,
});
You can flip multiple elements at the same time. In fact, you can add a stagger to stagger their start times.
Special properties
The Flip.from() options object (2nd parameter) can contain any of the following optional properties in addition to any standard tween properties like duration, ease, onComplete, etc. which are described elsewhere:
| Property | Description |
|---|---|
| absolute | Boolean | String | Array | NodeList | Element - specifies which of the targets should have |
| absoluteOnLeave | Boolean - if |
| fade | Boolean - by default, if the target element associated with a particular |
| nested | Boolean - if the Flip has any nested targets (like a parent and its child are both in the |
| onEnter | Function - A callback that's called if/when a target either isn't found in the original |
| onLeave | Function - A callback that's called if/when a target is in the original |
| props | String - a comma-delimited list of camelCased CSS properties that should be included in the flip animation beyond the standard positioning/size/rotation/skew ones. For example, |
| prune | Boolean - if |
| scale | Boolean - by default, Flip will affect the |
| simple | Boolean - if |
| spin | Boolean | Number | Function - if |
| targets | String | Element | Array | NodeList - by default, Flip will use the targets from the |
| toggleClass | String - adds a CSS class to the targets while the flip animation is in progress. For example |
| zIndex | Number - immediately sets the zIndex CSS property to this value for the entire course of the flip animation and then reverts at the end. This makes it easy to ensure that your flipping elements are on top of other elements during the animation, for example. | | |
Simple example
<button class="button">Swop element positions</button>
<div class="container">
<div class="square box gradient-blue" id="sq1">1</div>
<div class="square box gradient-pink" id="sq2">2</div>
</div>body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
p {
margin-top: 1rem;
}
.container {
display: flex;
justify-content: space-around;
align-items: center;
height: 100px;
width: 100%;
}
.box {
width: 80px;
height: 80px;
}
button {
margin: 2rem 0.5rem;
}gsap.registerPlugin(Flip);
const squares = gsap.utils.toArray(".square");
function doFlip() {
// Get the initial state
const state = Flip.getState(squares);
// Make DOM or styling changes (swap the squares in our case)
swap(squares);
// Animate from the initial state to the end state
Flip.from(state, {duration: 2, ease: "power1.inOut"});
}
// Given an Array of two siblings, append the one that's first so it's last (swap)
function swap([a, b]) {
a.parentNode.children[0] === a ? a.parentNode.appendChild(a) : a.parentNode.appendChild(b);
}
// click to flip
document.querySelector(".button").addEventListener("click", doFlip);Flex example
<h3>Smoothly change flex direction with GSAP's Flip plugin</h3>
<button id="changeLayout" class="button">change</button>
<div class="group">
<div class="box">Common "FLIP" techniques employed by other tools won't work with flex elements because of the way browsers handle width/height.
</div>
<div class="box">Simply set <code>absolute: true</code> to have GSAP's Flip plugin make the elements position: absolute during the flip to work around challenges with flex and grid layouts.
</div>
<div class="box">When the flip animation is done, elements get reverted back to their normal positioning and everything appears seamless.</div>
<div class="box">Happy flipping!</div>
</div>/* Typography */
@font-face {
font-display: block;
font-family: Mori;
font-style: normal;
font-weight: 400;
src: url(/fonts/Mori/PPMori-Regular.woff) format("woff");
}
@font-face {
font-display: block;
font-family: Mori;
font-style: normal;
font-weight: 600;
src: url(/fonts/Mori/PPMori-SemiBold.woff) format("woff");
}
@font-face {
font-display: block;
font-family: Fraktion Mono;
font-style: normal;
font-weight: 400;
src: url(/fonts/Fraktion/PPFraktionMono-Bold.woff)
format("woff");
}
:root {
--color-shockingly-green: #0ae448;
--color-just-black: #0e100f;
--color-surface-white: #fffce1;
--color-pink: #fec5fb;
--color-shockingly-pink: #f100cb;
--color-orangey: #ff8709;
--color-lilac: #9d95ff;
--color-lt-green: #abff84;
--color-blue: #00bae2;
--color-grey: gray;
--color-surface75: #bbbaa6;
--color-surface50: #7c7c6f;
--color-surface25: #42433d;
}
body {
color: var(--color-surface-white);
background-color: var(--color-just-black);
font-family: Mori, sans-serif;
font-weight: 400;
margin: 0;
font-size: 1rem;
line-height: 1.45;
text-align: center;
}
.group.reorder {
flex-direction: row;
}
h1 {
font-weight: 400;
}
.group {
width: 740px;
height: auto;
margin: 1rem auto;
padding: 4px;
border: dashed 2px var(--color-surface-white);
display: flex;
flex-direction: column;
overflow: hidden;
color: var(--color-just-black);
position: relative;
border-radius: 10px;
}
.box {
margin: 4px;
padding: 8px;
line-height: 28px;
border-radius: 10px;
}
.box:nth-child(1) {
background-color: #bef3fe;
}
.box:nth-child(2) {
background-color: var(--color-blue);
}
.box:nth-child(3) {
background-color: var(--color-pink);
}
.box:nth-child(4) {
background-color: #e1faff;
}
button {
margin: 0 auto;
display: inline-block;
outline: none;
padding: 8px 14px;
background: var(--dark);
border: solid 2px var(--color-surface-white);
color: var(--light);
text-decoration: none;
border-radius: 99px;
padding: 12px 25px;
text-transform: uppercase;
font-weight: 600;
cursor: pointer;
line-height: 18px;
}
.gsap-logo {
width: 20vw;
max-width: 90px;
position: fixed;
bottom: 15px;
right: 15px;
mix-blend-mode: difference;
z-index: 9999;
}
img {
max-width: 100%;
}gsap.registerPlugin(Flip);
const group = document.querySelector(".group");
document.querySelector(".button").addEventListener("click", () => {
// Get the initial state
const state = Flip.getState(".group, .box");
console.log(state);
// toggle the flex direction
group.classList.toggle("reorder");
Flip.from(state, {
absolute: true, // uses position: absolute during the flip to work around flexbox challenges
duration: 0.5,
stagger: 0.1,
ease: "power1.inOut"
// you can use any other tweening properties here too, like onComplete, onUpdate, delay, etc.
});
});How do I flip between two different elements?
Flip looks for a data-flip-id attribute on every element it interacts with (via Flip.getState() or Flip.from(), etc.) and if one isn't found, Flip assigns an incremented one automatically ("auto-1", "auto-2", etc.). It lets you correlate targets (the target with the data-flip-id of "5" in the "from" state gets matched up with the target with a data-flip-id of "5" in the end state). The data-flip-id can be any string, not just a number.
So if you want to flip between two different targets, make sure the data-flip-id attribute in the end state matches the one in the "from" state. When Flip sees that there are two with the same value in the from/end state, it will automatically figure out which one is disappearing (typically with display: none) and base things off of that to "swap" the elements. If you want them to crossfade, simply set fade: true, otherwise they'll immediately swap. And it is typically best to set absolute: true so that when Flip alters the display value, it doesn't affect the document flow.
<div class="initial">
<img class="thumbnail" data-flip-id="img" src="/demo-assets/flip-flair.webp">
</div>
<div class="container">
<img class="full-size" data-flip-id="img" src="/demo-assets/massive-flip-flair.webp">
</div>body {
padding: 0;
box-sizing: border-box;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
h1 {
margin-left: 20px;
}
.container {
position: fixed;
top: 15px;
left: 15px;
right: 15px;
bottom: 15px;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
pointer-events: none;
z-index: 999;
}
.full-size {
position: relative;
flex-shrink: 0;
flex-grow: 0;
height: 80%;
aspect-ratio: 1/1;
display: none;
}
.full-size.active {
display: block;
z-index: 2;
}
.thumbnail {
width: 150px;
height: 150px;
}
.initial {
position: relative;
width: 180px;
height: 180px;
border: dashed 2px var(--color-surface-white);
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
z-index: 1;
}
.initial:after {
content: "drag and click me...";
position: absolute;
background-image: url("data:image/svg+xml,%3Csvg width='100%25' height='100%25' viewBox='0 0 1516 1464' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve' xmlns:serif='http://www.serif.com/' style='fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;'%3E%3Cpath d='M481.106,1059.48c40.533,-60.792 74.612,-125.5 105.72,-191.542c33.38,-70.875 64.242,-142.917 98.3,-213.458c33.85,-70.084 70.951,-139 116.609,-202.209c4.479,-6.208 9.108,-12.333 13.75,-18.416c36.487,-46.334 78.041,-88.125 124.45,-124.542c64.5,-48.917 136.066,-87.458 210.354,-119.333c83.954,-35.042 170.958,-62.167 258.404,-87.042c25.217,-7.167 50.479,-14.125 75.754,-21.083c21.009,-5.792 36.03,-29.834 29.1,-51.25c-6.954,-21.5 -28.737,-35.292 -51.254,-29.125c-100.741,27.708 -201.579,56.041 -299.054,93.958c-101.329,39.417 -197.683,88.458 -283.421,155.792c-84.475,66.375 -151.204,151.666 -205.071,244.125c-53.637,92.042 -95.441,190.25 -139.691,286.958c-41.634,91 -86.405,180.75 -144.725,262.375c-5.73,8.042 -11.613,15.958 -17.588,23.792c-34.783,44.375 -73.808,85.5 -118.171,120.458c-31.387,23.583 -65.154,43.792 -101.121,59.584c-3.295,1.333 -6.608,2.625 -9.929,3.916c29.221,-57.958 56.104,-117.166 79.171,-177.792c15.171,-39.874 28.579,-80.583 37.846,-122.249c2.421,-10.875 1.529,-22.334 -4.2,-32.126c-5.058,-8.625 -14.967,-16.875 -24.9,-19.124c-10.667,-2.459 -22.642,-1.917 -32.108,4.166c-8.871,5.75 -16.788,14.292 -19.15,24.917c-12.555,56.458 -32.48,111.166 -54.746,164.5c-19.3,45.333 -40.504,89.875 -63.046,133.708c-15.325,29.792 -31.233,59.292 -47.879,88.375c-2.946,5.167 -5.9,10.292 -8.938,15.417c-6.841,11.458 -7.191,25 -2.1,37.083c4.634,10.959 14.567,21.917 27,24.125c102.446,18 204.888,36 307.329,54c28.688,5.042 57.371,10.083 86.055,15.125c11.287,1.959 21.891,1.75 32.104,-4.208c8.646,-5.084 16.887,-14.959 19.15,-24.917c2.429,-10.666 1.904,-22.625 -4.2,-32.083c-5.563,-8.625 -14.379,-17.334 -24.9,-19.167c-93.467,-16.416 -186.929,-32.833 -280.392,-49.25c62.683,-23.708 119.513,-59.458 170.283,-103.5c56.417,-48.916 103.888,-107.958 145.205,-169.958Z' style='fill:%23fffce1;fill-rule:nonzero;'/%3E%3C/svg%3E");
width:200px;
height: 72px;
background-size: 60px;
padding-left: 75px;
background-position: bottom left;
background-repeat: no-repeat;
text-align: left;
bottom: 100%;
margin-bottom: 0.2rem;
margin-left: 0.5rem;
left: 100%;
}
.thumbnail.active {
visibility: hidden;
}
.thumbnail,
.thumbnail.flipping {
visibility: visible;
}gsap.registerPlugin(Flip, Draggable);
const fullSize = document.querySelector(".full-size"),
thumbnail = document.querySelector(".thumbnail");
Draggable.create(".initial", {bounds: "body"});
document.addEventListener("click", () => {
const state = Flip.getState(".thumbnail, .full-size");
fullSize.classList.toggle("active");
thumbnail.classList.toggle("active");
Flip.from(state, {
duration: 0.6,
fade: true,
absolute: true,
toggleClass: "flipping",
ease: "power1.inOut"
});
});