学习css动画-animation

文章目录

  • 前言
  • 语法定义
    • @keyframe 关键帧
    • animation-*
  • 使用效果
    • scaleX X轴 左右两边扩散动画
    • scaleY Y轴 上下效果扩散动画
    • scale、rotate 中心为轴 旋转变化效果
    • translateY、opacity 中心为轴 透明度及上下变化效果
    • translateY、动画方法 上下抖动变化效果
    • `如有启发,可点赞收藏哟~`


前言

CSS Animation 才算是真正意义上的 CSS3 动画,它具备了对 关键帧和循环次数 的自定义能力。与 CSS Transition 相比较,有如下 CSS 过渡 所不具备的特性:

  • CSS Animation 在实现像 CSS Transition 补间动画 效果时,还可以在起始帧和结束帧之间自定义中间帧,使得动画更加平滑过渡的同时,对动画有了更好的控制和自定义能力。

  • CSS Animation 通过 animation-timing-function: steps() 属性实现了 CSS Transition 无法具备的 逐帧动画 效果

  • CSS Animation 只要定义了结束帧 (即 @keyframes 中的 to),在首屏渲染时,它默认会以指定元素在动画开始时刻的样式作为起始关键帧,并结合 to 定义的结束关键帧和指定元素的 animation 其他参数来完成补间动画的计算和动画帧的绘制。


语法定义

  • 使用 animation 属性或 其子属性 animation-*定义
  • 该属性允许配置动画时间、时长以及其他动画细节
  • 但该属性不能配置动画的实际表现,动画的实际表现由 @keyframes 规则实现

@keyframe 关键帧

创建一个带名称的 @keyframes 规则,以便后续使用 animation-name 属性将动画同其关键帧声明进行匹配。
每个规则包含多个关键帧

animation-*

属性 描述
animation-name 指定一个或多个 @keyframes 的名称,描述了要应用于元素的动画。多个 @keyframes 以逗号分隔的名称列表的形式指定。
animation-duration 设置动画完成一个动画周期所需的时间,需要指定单位,如 1s、500ms。
animation-delay 指定执行动画之前的等待时间。动画可以稍后开始、立即从开头开始、立即在动画中途播放 (如 -1s)。其中 -1s 意思是动画立即从 1s 处开始。
animation-iteration-count 设置动画序列在停止前应播放的次数,有效值 0、正整数、正小数、无限循环 infinite。
animation-direction 设置动画是正向播放 normal、反向播放 reverse、正向交替播放 alternate、反向交替播放 alternate-reverse。
animation-play-state 设置动画是运行还是暂停,有效值 running、paused。
animation-fill-mode 设置 CSS 动画在执行之前和之后如何将样式应用于其目标
animation-timing-function 设置动画在每个周期的持续时间内如何进行
实验性新属性
animation-range 设置动画附件范围沿其时间轴的开始和结束
animation-timeline 指定用于控制 CSS 动画进度的时间轴
animation-composition 指定当多个动画同时影响同一属性时要使用的复合操作。

获取直接简写animation 同transition类似,具体声明样式顺序如下

  • animation-duration
  • animation-easing-function
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-fill-mode
  • animation-play-state
  • animation-name

使用效果

以下例子均以此为原有样式

.box {
  width: 100px;
  height: 100px;
  margin: 18px auto;
  background-color: #1a1e23;
  animation: scle-move 0.6s both;
}

scaleX X轴 左右两边扩散动画

<template>
  <div class="box">div>
template>
<style scoped>
/* scaleX X轴 左右效果 */
@keyframes scle-move {
  from {
    transform: scaleX(0);
  }

  to {
    transform: scaleX(1);
  }
}
style>

scaleY Y轴 上下效果扩散动画

<template>
  <div class="box">div>
template>
<style scoped>
/* scaleY Y轴 上下效果 */
@keyframes scle-move {
  from {
    transform: scaleY(0);
  }

  to {
    transform: scaleY(1);
  }
}
style>

scale、rotate 中心为轴 旋转变化效果

<template>
  <div class="box" ref="box">div>
template>
<style scoped>
/* scale、rotate 中心为轴 旋转变化效果 */
@keyframes scle-move {
  from {
    transform: scale(0.3) rotate(0);
  }
  to {
    transform: scale(1) rotate(360deg);
  }
}
style>

translateY、opacity 中心为轴 透明度及上下变化效果

<template>
  <div class="box" ref="box">div>
template>
<style scoped>
/* translateY、opacity 中心为轴 透明度及上下变化效果 */
@keyframes scle-move {
  from {
    transform: translateY(-600px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}
style>

translateY、动画方法 上下抖动变化效果

<template>
  <div class="box" ref="box">div>
template>
<style scoped>

/* translateY、动画方法 上下抖动变化效果 */
@keyframes scle-move {
  0% {
    transform: translateY(-600px);
    animation-timing-function: ease-in;
  }

  30% {
    transform: translateY(0);
    animation-timing-function: ease-out;
  }

  50% {
    transform: translateY(-90px);
    animation-timing-function: ease-in;
  }

  65% {
    transform: translateY(0);
    animation-timing-function: ease-out;
  }

  80% {
    transform: translateY(-50px);
    animation-timing-function: ease-in;
  }

  90% {
    transform: translateY(0px);
    animation-timing-function: ease-out;
  }

  93% {
    transform: translateY(-28px);
    animation-timing-function: ease-in;
  }

  96% {
    transform: translateY(0px);
    animation-timing-function: ease-in;
  }

  99% {
    transform: translateY(-16px);
    animation-timing-function: ease-out;
  }

  100% {
    transform: translateY(0);
  }
}
style>

如有启发,可点赞收藏哟~

你可能感兴趣的:(前端代码相关知识点,学习,css,前端)