【CSS】前端三大件之一,如何学好?从基本用法开始吧!(九):强势分析Animation动画各类参数;从播放时间、播放方式、播放次数、播放方向、播放状态等多个方面,完全了解CSS3 Animation

本文涉及的产品
应用实时监控服务-可观测链路OpenTelemetry版,每月50GB免费额度
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: Animation属性css3为Animation动画提供的几个属性如下:属性名 属性值animation-name 指定动画名称,该属性指定一个已有的关键帧定义。animation-duration 指定动画持续时间。animation-timing-funtion 指定动画变化速度。animation-delay 指定动画延迟多长时间才开始执行。animation-iteration-count 指定动画的循环执行次数。animation:这是一个复合属性。

Animation动画

Animation属性

css3为Animation动画提供的几个属性如下:
|属性名|属性值|
|--|--|
|animation-name|指定动画名称,该属性指定一个已有的关键帧定义。|
|animation-duration|指定动画持续时间。|
|animation-timing-funtion|指定动画变化速度。|
|animation-delay|指定动画延迟多长时间才开始执行。|
|animation-iteration-count|指定动画的循环执行次数。|

animation:这是一个复合属性。

  • 该属性的格式为
    animation: animation-name animation-duration animation-timing-funciotn animation-delay animation-iteration-count.

keyframes(关键帧):计算机动画术语

  • 帧——就是动画中最小单位的单幅影像画面,
  • 相当于电影胶片上的每一格镜头。在动画软件的时间轴上帧表现为一格或一个标记。
    关键帧——相当于二维动画中的原画。指角色或者物体运动或变化中的关键动作所处的那一帧。
    关键帧与关键帧之间的动画可以由软件来创建,叫做过渡帧或者中间帧。

Keyframes被称为关键帧,其类似于Flash中的关键帧。
在CSS3中其主要以“@keyframes”开头,
后面紧跟着是动画名称加上一对花括号“{…}”,括号中就是一些不同时间段样式规则。

@keyframes changecolor{
   
  0%{
   
   background: red;
  }
  50%{
   
   background: red;
  }
  100%{
   
    background: green;
  }
}

在一个“@keyframes”中的样式规则可以由多个百分比构成的,如在“0%”到“100%”之间创建更多个百分比,分别给每个百分比中给需要有动画效果的元素加上不同的样式,从而达到一种在不断变化的效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>01Keyframes</title>
    <style>
        @keyframes changecolor{
    
    0%{
    
        background:#E82733;
    }
    20%{
    
        background:#E038BA;
    }
    40%{
    
        background:#482EB1;
    }
    60%{
    
        background:#1DDD2D;
    }
    100%{
    
        background:#E3E834;
    }
}

div {
    
    width: 300px;
    height: 300px;
    margin: 100px auto;
    line-height: 300px;
    text-align: center;
    color: #fff;
    font-size: 20px;
    background: #0F3;
}
div:hover{
    
    animation-name:changecolor;
    animation-duration:3s;
    animation-timing-function:ease-in;
    animation-delay:.4s;
    animation-iteration-count:3;
    /*animation:changecolor 3s ease-in 0.3s 2;*/
}

    </style>
</head>

<body>
    <div>鼠标放在我这儿,看变化</div>
</body>
</html>

animation-name 调用动画

animation-name属性主要是用来调用 @keyframes 定义好的动画。
需要特别注意: animation-name 调用的动画名需要和“@keyframes”定义的动画名称完全一致(区分大小写),
如果不一致将不具有任何动画效果。
属性值:
none:默认值。当值为 none 时,将没有任何动画效果,这可以用于覆盖任何动画。
@keyframes 定义的动画名称。

@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}

@keyframes around {
   
     0%{
   
     transform:translateX(0);
    }
     25% {
   
     transform: translateX(180px);
    }
     50% {
   
     transform: translate(180px, 180px);
    }
     75% {
   
     transform:translate(0, 180px);
    }
     100% {
   
     transform: translateY(0);
    }
}
div {
   
    width: 200px;
    height: 200px;
    border: 2px solid #C0F;
    margin: 100px auto;
}
div span {
   
    display: block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: #93C;
    animation-name:around;
    animation-duration:10s;
    animation-timing-function:ease;
    animation-delay:.3s;
    animation-iteration-count:5;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>02调用动画</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
    <div><span></span></div>
</body>
</html>

animation-duration 播放时间

animation-duration主要用来设置CSS3动画播放时间,其使用方法和transition-duration类似,是用来指定元素播放动画所持续的时间长,也就是完成从0%到100%一次动画所需时间。

  • 单位:S秒
@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}
@keyframes toradius{
   
  from {
   
    border-radius: 0;
  }
  to {
   
    border-radius: 50%;
  }
}
div {
   
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
  color: #fff;
  background: green;
  margin: 20px auto;

  animation-name:toradius;
  animation-duration:2s;
  animation-timing-function:linear;
  animation-delay:.5s;
  animation-iteration-count:infinite;
}
div:hover{
   
    animation-play-state:paused;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>03设置动画播放时间</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div>Hover Me</div>
</body>
</html>

animation-timing-function 动画播放方式

主要让元素根据时间的推进来改变属性值的变换速率,简单点说就是动画的播放方式。
|参数|说明|
|--|--|
|ease|默认值,动画开始时比较慢,然后加快速度,达到最大速度后再减慢速度。|
|linear|线性速度。动画开始时的速度和结束时的速度一样(匀速)。|
|ease-in|动画开始的速度较慢,然后速度加快。|
|ease-out|动画开始的速度很快,然后速度减慢。|
|ease-in-out|动画开始时比较慢,然后加快速度,达到最大速度后再减慢速度.|

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>04设置动画播放方式</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div><span></span></div>
</body>
</html>
@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}
@keyframes move {
   
  0%{
   
    transform: translate(0);
  }
  15%{
   
    transform: translate(100px,180px);
  }
  30%{
   
    transform: translate(150px,0);
  }
  45%{
   
    transform: translate(250px,180px);
  }
  60%{
   
    transform:translate(300px,0);
  }
  75%{
   
    transform: translate(450px,180px);
  }
  100%{
   
    transfrom: translate(480px,0);
  }
}
div{
   
    width:500px;
    height:200px;
    border:2px solid #903;
    margin:100px auto;
}
div span{
   
    display:inline-block;
    width:20px;
    height:20px;
    background:#C0F;
    border-radius: 100%;
    animation-name:move;
    animation-duration:10s;
    animation-timing-function:ease-out;
}

animation-iteration-count 动画播放次数

  • 其值通常为整数,但也可以使用带有小数的数字,其默认值为1,
    这意味着动画将从开始到结束只播放一次。

  • 如果取值为infinite,动画将会无限次的播放。

@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}
@keyframes move {
   
  0%{
   
    transform: translate(0);
  }
  15%{
   
    transform: translate(100px,180px);
  }
  30%{
   
    transform: translate(150px,0);
  }
  45%{
   
    transform: translate(250px,180px);
  }
  60%{
   
    transform:translate(300px,0);
  }
  75%{
   
    transform: translate(450px,180px);
  }
  100%{
   
    transfrom: translate(480px,0);
  }
}
div{
   
    width:500px;
    height:200px;
    border:2px solid #903;
    margin:100px auto;
}
div span{
   
    display:inline-block;
    width:20px;
    height:20px;
    background:#C0F;
    border-radius: 100%;
    animation-name:move;
    animation-duration:10s;
    animation-timing-function:ease-out;
    animation-iteration-count:infinite;
    /*animation:move 10s ease-in infinite;*/
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>06设置动画播放的次数</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div><span></span></div>
</body>
</html>

animation-direction 动画播放方向

其主要有两个值:normal、alternate

  1. normal是默认值,如果设置为normal时,动画的每次循环都是向前播放;
  2. 另一个值是alternate,他的作用是,动画播放在第偶数次向前播放,第奇数次向反方向播放。

例如:通过animation-direction属性,
将move动画播放动画方向设置为alternate,代码为:
animation-direction:alternate;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>07设置动画播放的方向</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div><span></span></div>
</body>
</html>
@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}
@keyframes move {
   
  0%{
   
    transform: translateY(90px);
  }
  15%{
   
    transform: translate(90px,90px);
  }
  30%{
   
    transform: translate(180px,90px);
  }
  45%{
   
    transform: translate(90px,90px);
  }
  60%{
   
    transform: translate(90px,0);
  }
  75%{
   
    transform: translate(90px,90px);
  }
  90%{
   
    transform: translate(90px,180px);
  }
  100%{
   
    transform: translate(90px,90px);
  }
}
div {
   
  width: 200px;
  height: 200px;
  border: 1px solid red;
  margin: 20px auto;
}
div span {
   
  display: inline-block;
  width: 20px;
  height: 20px;
  background: #03F;
  transform: translateY(90px);
  animation-name: move;
  animation-duration: 10s;
  animation-timing-function: ease-in;
  animation-delay: .2s;
  animation-iteration-count:infinite;
  animation-direction:alternate;

 /* animation-direction:normal;*/
}
div span:hover{
   
    animation-play-state:paused;
}

animation-play-state 元素动画的播放状态

其主要有两个值:runningpaused
其中running是其默认值,主要作用就是类似于音乐播放器一样,
可以通过paused将正在播放的动画停下来,也可以通过running将暂停的动画重新播放

  • 这里的重新播放不一定是从元素动画的开始播放,而是从暂停的那个位置开始播放。另外如果暂停了动画的播放,元素的样式将回到最原始设置状态。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>08设置动画播放状态</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div><span></span></div>
</body>
</html>
@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}
@keyframes move {
   
  0%{
   
    transform: translateY(90px);
  }
  15%{
   
    transform: translate(90px,90px);
  }
  30%{
   
    transform: translate(180px,90px);
  }
  45%{
   
    transform: translate(90px,90px);
  }
  60%{
   
    transform: translate(90px,0);
  }
  75%{
   
    transform: translate(90px,90px);
  }
  90%{
   
    transform: translate(90px,180px);
  }
  100%{
   
    transform: translate(90px,90px);
  }
}
div {
   
  width: 200px;
  height: 200px;
  border: 1px solid red;
  margin: 20px auto;
}
div span {
   
  display: inline-block;
  width: 20px;
  height: 20px;
  background: #03F;
  transform: translateY(90px);
  animation-name: move;
  animation-duration: 10s;
  animation-timing-function: ease-in;
  animation-delay: .2s;
  animation-iteration-count:infinite;
  animation-direction:normal;
  animation-play-state:paused;/*加载时暂定*/
 /* animation-direction:normal;*/
}
div:hover span{
   
    animation-play-state:running;
}

animation-fill-mode 定义在动画开始之前和结束之后发生的操作

主要具有四个属性值:noneforwardsbackwordsboth。其四个属性值对应效果如下:

参数 说明
none 默认值,表示动画将按预期进行和结束,在动画完成其最后一帧时,动画会反转到初始帧处
forwards 表示动画在结束后继续应用最后的关键帧的位置
backwards 会在向元素应用动画样式时迅速应用动画的初始帧
both 元素动画同时具有forwards和backwards效果

在默认情况之下,动画不会影响它的关键帧之外的属性,使用animation-fill-mode属性可以修改动画的默认行为。

简单的说就是告诉动画在第一关键帧上等待动画开始,或者在动画结束时停在最后一个关键帧上而不回到动画的第一帧上。或者同时具有这两个效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>09设置动画时间外属性</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div></div>
</body>
</html>
@charset "utf-8";
/* CSS Document */
* {
   
    margin: 0;
    padding: 0;
}
@keyframes redToBlue{
   
  from{
   
    background: red;
  }
  20%{
   
      background:green;
  }
  40%{
   
      background:lime;/*绿黄色*/
  }
  60%{
   
      background:yellow;
  }
  to{
   
    background:blue;
  }
}

div {
   
  width: 200px;
  height: 200px;
  background: red;
  margin: 20px auto;
  animation-name:redToBlue;
  animation-duration: 2s;
  animation-timing-function: ease;
  animation-delay:.2s;
   /*  animatin-fill-mode: both; */
  -webkit-animation-fill-mode:both;
    animation-fill-mode:both;
}

🤩CSS教学往期回顾

  1. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(一):CSS发展史;CSS样式表的引入;CSS选择器使用,附带案例介绍
  2. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(二):CSS伪类:UI伪类、结构化伪类;通过伪类获得子元素的第n个元素;创建一个伪元素展示在页面中;获得最后一个元素;处理聚焦元素的样式
  3. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(三):元素继承关系、层叠样式规则、字体属性、文本属性;针对字体和文本作样式修改
  4. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(四):元素盒子模型;详细分析边框属性、盒子外边距
  5. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(五):背景属性;float浮动和position定位;详细分析相对、绝对、固定三种定位方式;使用浮动并清除浮动副作用
  6. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(六):全方面分析css的Flex布局,从纵、横两个坐标开始进行居中、两端等元素分布模式;刨析元素间隔、排序模式等
  7. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(七):学习ransform属性;本文学习 rotate旋转、scale缩放、skew扭曲、tanslate移动、matrix矩阵 多个参数
  8. 【CSS】前端三大件之一,如何学好?从基本用法开始吧!(八):学习transition过渡属性;本文学习property模拟、duration过渡时间指定、delay时间延迟 等多个参数

    💕👉博客专栏

目录
相关文章
|
1月前
|
前端开发 JavaScript 算法
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(八):学习transition过渡属性;本文学习property模拟、duration过渡时间指定、delay时间延迟 等多个参数
transition过渡属性 早期在Web中要实现动画效果,都是依赖于JavaScript或Flash来完成。 但在CSS3中新增加了一个新的模块transition,它可以通过一些简单的CSS事件来触发元素的外观变化, 让效果显得更加细腻。简单点说,就是通过鼠标经过、获得焦点,被点击或对元素任何改变中触发, 并平滑地以动画效果改变CSS的属性值。 在CSS中创建简单的过渡效果可以从以下几个步骤来实现: 在默认样式中声明元素的初始状态样式; 声明过渡元素最终状态样式,比如悬浮状态; 在默认样式中通过添加
154 0
|
1月前
|
前端开发 JavaScript 算法
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(七):学习ransform属性;本文学习 rotate旋转、scale缩放、skew扭曲、tanslate移动、matrix矩阵 多个参数
transform变形 css3在原来的基础上新增了变形和动画相关属性,通过这些属性可以实现以前需要大段JavaScript才能实现的 功能。 CSS3的变形功能可以对HTML组件执行位移、旋转、缩放、倾斜4种几何变换,这样的变换可以控制HTML组件 呈现出丰富的外观。 借助于位移、旋转、缩放、倾斜这4种几何变换,CSS3提供了transition动画。 transition动画比较简单,只要指定HTML组件的哪些CSS属性需要使用动画效果来执行变化,并指定动画时间,就可保证动画播放。 比transitio
117 0
|
1月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(六):全方面分析css的Flex布局,从纵、横两个坐标开始进行居中、两端等元素分布模式;刨析元素间隔、排序模式等
Flex 布局 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性。它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。 一、Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。
213 0
|
5月前
|
存储 自然语言处理 前端开发
抖音快手小红书虚拟评论截图生成器,模拟对话制作工具,html+js+css
这是一款纯前端实现的多平台虚拟评论生成器,支持抖音、快手、小红书风格,适用于产品演示与UI设计。采用Vanilla JS与Flexbox布局,利用IndexedDB存储数据,CSS Variables切换主题。
|
5月前
|
存储 前端开发 安全
病历单生成器在线制作,病历单生成器app,HTML+CSS+JS恶搞工具
本项目为医疗病历模拟生成器,旨在为医学教学和软件开发测试提供数据支持,严格遵守《医疗机构病历管理规定》。
|
5月前
|
存储 前端开发 JavaScript
仿真银行app下载安装, 银行卡虚拟余额制作app,用html+css+js实现逼真娱乐工具
这是一个简单的银行账户模拟器项目,用于学习前端开发基础。用户可进行存款、取款操作,所有数据存储于浏览器内存中
|
5月前
|
前端开发 容器
处方单图片生成器, 处方单在线制作免费,js+css+html恶搞神器
这是一个电子处方模拟生成系统,使用html2canvas库实现图片导出功能。系统生成的处方单包含多重防伪标识,并明确标注为模拟数据,仅供学习
|
5月前
|
前端开发 JavaScript 容器
制作b超单生成器, 假怀孕b超单图片制作, p图医院证明【css+html+js装逼恶搞神器】
本资源提供一个适合用于熟人之间恶搞的工具,效果逼真,仅供学习参考与娱乐。包含前端技术学习要点:语义化布局、响应式设计、Flexbox、图片自适应
|
5月前
|
前端开发
医院检查单子p图软件,在线制作仿真病历,js+css+html装逼神器
本示例展示如何用HTML/CSS创建医疗信息页面,内容仅供学习参考。页面模拟“阳光医院体检中心”场景,提供预约功能验证(如姓名、手机号、日期)。所有数据仅用于演示