Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

CSS Animations

Vizia supports CSS keyframe animations defined with @keyframes rules and the animation property. These run entirely in CSS and do not require Rust code to play.

Defining a keyframe animation

Declare @keyframes in a stylesheet and attach the animation with the animation property:

use vizia::prelude::*;

const STYLE: &str = r#"
    @keyframes spin {
        from { transform: rotate(0deg); }
        to   { transform: rotate(360deg); }
    }

    .spinner {
        animation: spin 1s linear infinite;
    }
"#;

fn main() -> Result<(), ApplicationError> {
    Application::new(|cx| {
        cx.add_stylesheet(STYLE).unwrap();

        Element::new(cx)
            .class("spinner")
            .size(Pixels(32.0));
    })
    .run()
}

Animation shorthand

The animation shorthand accepts: name duration timing-function delay iteration-count direction fill-mode.

.fade-in {
    animation: fadeIn 300ms ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

Iteration count

ValueDescription
1Plays once.
3Plays three times.
infiniteLoops forever.

Direction

ValueDescription
normalPlays forward each iteration.
reversePlays backward each iteration.
alternateAlternates forward and backward.
alternate-reverseAlternates backward then forward.

Fill mode

ValueDescription
noneReturns to pre-animation state after end.
forwardsKeeps the final keyframe state.
backwardsApplies the first keyframe before the delay elapses.
bothApplies forwards and backwards together.

Multiple keyframe stops

Use percentage-based keyframes for fine-grained control:

@keyframes bounce {
    0%   { transform: translateY(0px); }
    40%  { transform: translateY(-20px); }
    60%  { transform: translateY(-10px); }
    80%  { transform: translateY(-5px); }
    100% { transform: translateY(0px); }
}

See also

  • Animations — Rust API animations with AnimationBuilder.
  • Transitions — property transitions triggered by state changes.