CSS动画基础

CSS 动画

创建动画序列,需要使用 animation 属性或其子属性,该属性允许配置动画时间、时长以及其他动画细节,但该属性不能配置动画的实际表现,动画的实际表现是由 @keyframes 规则实现。

CSS animation

  • CSS animation 属性是 animation-nameanimation-duration, animation-timing-functionanimation-delayanimation-iteration-countanimation-directionanimation-fill-modeanimation-play-state 属性的一个简写属性形式。
  • animation 属性用来指定一组或多组动画,每组之间用逗号相隔。
  • 初始值:
    • animation-name: none
    • animation-duration: 0s
    • animation-timing-function: ease
    • animation-delay: 0s
    • animation-iteration-count: 1
    • animation-direction: normal
    • animation-fill-mode: none
    • animation-play-state: running
    • animation-timeline (en-US): auto

animation 的子属性:

使用 keyframes 定义动画序列

一旦完成动画的时间设置, 接下来就需要定义动画的表现。通过使用 @keyframes 建立两个或两个以上关键帧来实现。每一个关键帧都描述了动画元素在给定的时间点上应该如何渲染。

因为动画的时间设置是通过 CSS 样式定义的,关键帧使用 percentage 来指定动画发生的时间点。0% 表示动画的第一时刻,100% 表示动画的最终时刻。因为这两个时间点十分重要,所以还有特殊的别名:fromto。这两个都是可选的,若 from/0%to/100% 未指定,则浏览器使用计算值开始或结束动画。

也可包含额外可选的关键帧,描述动画开始和结束之间的状态。

示例

  • 动画:上下浮动。
  • 代码:
<div class="box slide"/>
.box {
  width: 300rpx;
  height: 160rpx;
  background-color: rgb(65, 114, 98);
}

.slide {
  animation: slide 1.5s  linear infinite both;
  -webkit-animation: slide 1.5s  linear infinite both;
}

@-webkit-keyframes slide {
  25% {
    transform: translateY(-4px);
    transform: translateY(-4px);
  }

  50%,
  100% {
    transform: translateY(0);
    transform: translateY(0);
  }

  75% {
    transform: translateY(4px);
    transform: translateY(4px);
  }
}

@keyframes slide {
  25% {
    transform: translateY(-4px);
    transform: translateY(-4px);
  }

  50%,
  100% {
    transform: translateY(0);
    transform: translateY(0);
  }

  75% {
    transform: translateY(4px);
    transform: translateY(4px);
  }
}

END

参考文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值