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
state
object, 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
loading...
Flex example
loading...
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.