css3中animation 属性

animation: name duration timing-function delay iteration-count direction;

animation 属性是一个简写属性,用于设置六个动画属性:

  • animation-name:规定需要绑定到选择器的 keyframe 名称。(默认值为none)
  • animation-duration:规定完成动画所花费的时间,以秒或毫秒计。(默认值为0)
  • animation-timing-function:规定动画的速度曲线。(默认值为ease)
  • animation-delay:规定在动画开始之前的延迟。(默认值为0)
  • animation-iteration-count:规定动画应该播放的次数。(默认值为1)
  • animation-direction:规定是否应该轮流反向播放动画。(默认值为normal)

注释:请始终规定 animation-duration 属性,否则时长为 0,就不会播放动画了。

div{
    width: 300px;
    height: 100px;
    background-color: pink;
    animation-name: test1,test2;
    animation-duration: 3s;
    animation-iteration-count: infinite;
}
@keyframes test2{
    0%{background-color: blue;}
    30%{background-color: green;}
    60%{background-color: gray;}
    100%{background-color: black;}
}
### CSS3 `animation` 属性使用教程 #### 一、常见动画属性简介 CSS3 提供了一系列用于创建复杂动画效果的属性。其中最核心的是 `animation` 组合属性,它能够一次性设定多个子属性来控制动画的行为[^2]。 - **`animation-name`**: 定义应用到目标元素上的关键帧名称。 - **`animation-duration`**: 设置完成整个动画循环所需的时间长度,默认单位为秒(s),也可以使用毫秒(ms)[^4]。 - **`animation-timing-function`**: 描述动画的速度曲线,常见的有线性(`linear`)、缓入/缓出(`ease-in`, `ease-out`, `ease-in-out`)等选项[^1]。 - **`animation-delay`**: 指定动画开始前等待的时间间隔。 - **`animation-iteration-count`**: 控制动画重复播放次数;可设为无限次(`infinite`)。 - **`animation-direction`**: 决定了每次迭代的方向变化模式,比如正向运行还是交替反向执行。 - **`animation-fill-mode`**: 影响动画之外的状态,在未启动或结束后如何影响被选中的元素样式[^3]。 #### 二、代码示例 - 创建简单的淡入淡出效果 下面是一个利用上述提到的各种参数组合而成的例子: ```css /* 定义名为 'fade' 的 @keyframes */ @keyframes fade { from { opacity:0; } to { opacity:1; } } /* 应用该动画至特定类名 .example 上 */ .example { /* 单独指定各子属性 */ animation-name: fade; animation-duration: 2s; animation-timing-function: ease-in; animation-delay: 1s; animation-iteration-count: infinite; animation-direction: alternate; animation-fill-mode: both; /* 或者采用简写的复合形式 */ /* animation: fade 2s ease-in 1s infinite alternate both; */ } ``` 此段代码实现了当页面加载完成后的一秒钟延迟后,`.example` 类下的HTML元素将以两秒内逐渐变得完全可见的方式不断切换透明度状态,并且在动画前后保持最终的效果显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值