无限数量元素的 CSS 顺序动画级联

标签 css

据我所知,获取级联动画的常用方法是:

.box {
    animation: someAnimation 3s linear 0s 1 normal none;
}

然后如果我想实现级联(顺序)效果:

.box:nth-child(1) {
    animation-delay: 0s;
}

.box:nth-child(2) {
    animation-delay: 0.5s;
}

.box:nth-child(3) {
    animation-delay: 1s;
}

etc...

通常,这可以使用 LESS 或 SASS 自动生成,通过一个 mixin 输出 N 个元素。

问题是:我如何在 CSS 中对无限数量的元素执行此操作? 换句话说,如何在不为每个 child 指定延迟的情况下制作级联动画?

最佳答案

你可以使用 Sass 的数学属性:

fiddle here

通过变量指定nth-child,并在确定延迟时使用该变量。

@for $i from 1 through $total {
  .box:nth-child(#{$i}){
    -webkit-animation-delay: $i * 0.5s;
    animation-delay: $i * 0.5s - 0.5s;
  }
}

其中 $total 由 child 的数量定义。

关于无限数量元素的 CSS 顺序动画级联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25577170/

相关文章:

Jquery 只针对点击类的父类而不是其他类?

html - 容器中的 CSS 列超过列数

php - 只分页一次

html - CSS Transform origin 在 Firefox 中不起作用

HTML布局问题...

css - 使文本从底部开始

html - css 内联 block 图像列表 slider

javascript - 没有缩放的全屏 SVG

html - Umbraco 不渲染 IE 框阴影

css - 如何停止环绕菜单 div 的 div