🎷The Swing Animation
The swing animation is an animation that rotates the content of a transition group. For live examples visit the animation demo page and select one of the swing
presets in the button grid at the top of the page.
Contents
Why would I use this component?
Astro allows users to create their own animations for view transitions. The astro-vtbot
swing animation is a ready-made solution that can be easily inserted into Astro’s transition:animate
property. It also supports parameters for fine-tuning the animation to your needs. It is even possible to customize the underlying keyframes when used together with the <AnimationStyle/>
component.
Usage
Install astro-vtbot in your project running npx astro add astro-vtbot
or npm install astro-vtbot
.
Basic usage
The most basic usage is to apply the swing()
animation without any parameters. This will use the default values for the animation.
The effect will rotate the old-image 90° around the y axis. When it completely vanished, the animation for the new-image kicks in and again rotates 90° back to the full view. The animation will take 0.15 seconds and use the ease-in-out
easing function.
❗ Perspective
Please note that 3D-effects require the CSS perspective
property to be set on a parent of the transformed element.
For browsers that natively support view transition good choices for that parent are the ::view-transition-group
or ::view-transition-image-pair
pseudo element, which is the parent of the ::view-transition-old
and ::view-transition-new
pseudo elements.
The animations offered by the Bag of Tricks generate this for you, so you are already save on browsers that natively support view transitions.
For browsers that do not natively support view transitions, there are no such pseudo elements. For Astro’s fallback mode for view transitions to work properly with 3D animations, you have to set the perspective
on a regular HTML element that is a parent of the animated element in the DOM.
Here is how this is done in the animation demo:
Clipping
The same applies to clipping: To support browsers that do not implement native view transitions, set overflow
to clip
on a parent element in the DOM. For browsers that support native view transitions, set it to the ::view-transition-group
or ::view-transition-image-pair
pseudo-elements. Unlike perspective
, overflow
is not set by default as it is not clear whether you want clip
or visible
. In future versions you may be able to set this with a parameter.
Parametrized usage
The default values for the animation can be overwritten by passing an object with the desired values to the swing()
function. The following example shows how to change the duration and easing of the animation.
You can set the following parameters:
Property name | CSS property | Default value |
---|---|---|
duration | animation-duration | 0.15s |
easing | animation-timing-function | ease-in-out |
delay | animation-delay | (see below) |
direction | animation-direction | normal |
fillMode | animation-fill-mode | both |
The default value for delay
is 0 for the out-animation of the old image. If not specified, the delay
for the in-animation of the new image uses the duration
value. The in-animation therefore begins as soon as the out-animation is complete. If you explicitly set a value for the delay, this value is used for both the in and out animation. This is typically not what you want. If you want to use different values for the in and out animation, use the customSwing
function as described in the next section.
Extended parametrized usage
If you want to use more than the 5 properties supported by swing()
or want to have more control over the animations keyframes, you can use the customSwing()
function. This function takes a name for the transition and an object with the following optional properties:
keyframes
: An object with parameters for the keyframes for the animation.base
: An object with the same properties as theswing()
function.extensions
: An object with arbitrary CSS properties to be used in the forwards/backwards old/new animation.
The function returns a scope identifier to be used with the data-astro-transition-scope
attribute of the HTML element you want to animate. Under the hood, customSwing()
generates a style sheet with keyframe information and the defined CSS properties. You need to add these extended styles to your page by using the <AnimationStyle/>
component. Note: Only use <AnimationStyle/>
in conjunction with customSwing()
, not with swing()
.
Keyframes
The keyframes object of the customSwing()
function has the following properties:
The default values are all 0 with the exception of
opacity.mid = 1
,angle.leave = "90deg"
andangle.enter = "-90deg"
.
The values are used as parameters of the keyframe definition in
this style sheet template
Base
The value of the base
property is used to call the swing()
function as defined above.
Extensions
With the extensions
property you can define all those CSS properties that can not be defined by swing()
directly. You can specify different values for forwards
and backwards
, in
and out
animations. These values are merged with the result of callingswing()
with base parameters and overwrite its properties.