Basic animation is simple: define keyframes, name animations, and apply them to elements. But sometimes more complex techniques are required to achieve the ideal animation effect. For example, a sound equalizer might use the same animation for each bar, but they are interleaved to create the illusion of independent animations.
Recently when building a dashboard, I want the items of one of the widgets to be displayed in an interlaced animation.
Like the sound equalizer above, I started using the :nth-child
selector. I use an unordered list (<ul></ul>
) As the parent container, give it a class and use the :nth-child
pseudo-selector to offset the animation of each list item by animation-delay
.
.my-list li { animation: my-animation 300ms ease-out; } .my-list li:nth-child(1) { animation-delay: 100ms; } .my-list li:nth-child(2) { animation-delay: 200ms; } .my-list li:nth-child(3) { animation-delay: 300ms; } /* and so on*/
This technique does stagger items really well, especially if you know how many items are in the list at any given time. However, this approach fails when the number of items is unpredictable (this is the case with the widgets I built for the dashboard). I really don't want to return to this code every time the number of items in the list changes, so I wrote a quick Sass loop that can handle up to 50 items and add animation latency as each item increases:
.my-list { li { animation: my-animation 300ms ease-out; @for $i from 1 through 50 { &:nth-child(#{$i}) { animation-delay: 100ms * $i; } } } }
This should solve the problem! However, it feels too clumsy. Of course, it won't add too much weight to the file, but you know that compiled CSS will contain many unused selectors, such as nth-child(45)
.
There must be a better way. Usually I would turn to JavaScript to find all the projects and add delays, but… this time I spent some time exploring if it could be done alone with CSS.
How about CSS counters?
The first thing that comes to mind is to use a CSS counter with the calc()
function:
.my-list { counter-reset: my-counter; } .my-list li { counter-increment: my-counter; animation-delay: calc(counter(my-counter) * 100ms); }
Unfortunately, this doesn't work, as the specification states that the counter cannot be used in calc()
:
The component of
calc()
expression can be a literal value orattr()
orcalc()
expression.
It turns out that some people like the idea, but it is not out of the draft stage yet.
How about data attributes?
After reading the excerpt from the specification, I learned that calc()
can use attr()
. And, according to the CSS value and unit specification:
In CSS3, the
attr()
expression can return many different types of values
This reminds me; maybe the data attributes can solve the problem.
- Item 1
- Item 2
- Item 3
- Item 4
.my-list li { animation-delay: calc(attr(data-count) * 150ms); }
But my hopes are dashed because this browser support is very bad!
This browser supports data from Caniuse, which contains more details. The number indicates that the browser supports this feature in this version and later.
desktop
Mobile/Tablet PC
So, back to the drawing board.
How about custom properties?
My next idea is to use CSS to customize properties. It's not pretty, but it works?
It turns out it is flexible, too. For example, animations can be reversed:
It can also do something completely random and animate elements at the same time:
We can even go a step further and do a diagonal swing:
Browser support is not that bad ( pop up Internet Explorer ).
This browser supports data from Caniuse, which contains more details. The number indicates that the browser supports this feature in this version and later.
desktop
Mobile/Tablet PC
A great feature of CSS is that it ignores things it doesn't understand, thanks to cascading. This means that everything will be animate into the view together. If this is not your style, you can add a feature query to override the default animation:
.my-list li { animation: fallback-animation; } @supports (--variables) { .my-list li { animation: fancy-animation; animation-delay: calc(var(--animation-order) * 100ms); } }
Native CSS Long live
The more I paused to ask myself if I needed JavaScript, the more I was surprised at what CSS itself could do. Of course, if the CSS counter can be used in the calc()
function, that would be a very elegant solution. But for now, inline custom properties provide a powerful and flexible way to solve this problem.
The above is the detailed content of Different Approaches for Creating a Staggered Animation. For more information, please follow other related articles on the PHP Chinese website!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
