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

简介: 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时间延迟 等多个参数

    💕👉博客专栏

目录
相关文章
|
6月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(六):全方面分析css的Flex布局,从纵、横两个坐标开始进行居中、两端等元素分布模式;刨析元素间隔、排序模式等
Flex 布局 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性。它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。 一、Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。
426 0
|
6月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(五):背景属性;float浮动和position定位;详细分析相对、绝对、固定三种定位方式;使用浮动并清除浮动副作用
position定位(核心) 我们讲盒模型的时候,提到了3个属性可以用来控制页面排版。 三大属性:position属性,display属性,float属性。 position 属性控制页面上元素间的位置关系。 display 属性控制页面元素是否显示或者是堆叠还是并排显示。 float 属性提供控制方法。 通过float这种控制方法,可以实现多栏布局,导航菜单等等。 position属性是干嘛用的?怎么用?有哪些属性值? position属性控制页面上元素间的位置关系,也就是排版。 怎么用?要知道怎么用
609 1
|
6月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(四):元素盒子模型;详细分析边框属性、盒子外边距
盒模型 盒模型: 所谓盒模型,就是浏览器为页面中的每个HTML元素生成的矩形盒子。 这些盒子们都要按照可见板式模型在页面上排布。 可见的板式模型主要由三个属性控制:position 属性、display 属性和 float属性。 position属性控制页面上元素间的位置关系。 display属性控制元素是堆叠、并排或者不在页面上显示。 float属性提供控制的方法,以便于把元素组成成多栏布局。 盒模型讲解: 在默认的情况下,每个盒子的边框是不可见的,背景也是透明的。 所以我们 不能直接的看到页面中的盒
795 0
|
9月前
|
自然语言处理 前端开发 JavaScript
用 通义灵码 一键生成“水波纹按钮”,连 CSS 动画都不用自己写了!
通义灵码是一款智能编程辅助工具,它可以根据自然语言指令自动生成高质量的代码。例如,只需输入“生成一个按钮,点击时带水波纹动画,模拟 Material Ripple 效果”,它就能生成具备完整交互逻辑、CSS 动画和良好性能的按钮组件。不仅如此,它还支持拓展功能,如长按触发提示、添加图标等,并能自动优化样式适配不同场景。通过通义灵码,开发者可以大幅提升效率,专注于创意实现,而不必纠结于繁琐的代码细节。
|
前端开发 JavaScript
CSS 过渡和动画
CSS过渡和动画是用于为网页元素添加动态效果的两种重要技术
647 143
|
10月前
|
存储 前端开发 JavaScript
仿真银行app下载安装, 银行卡虚拟余额制作app,用html+css+js实现逼真娱乐工具
这是一个简单的银行账户模拟器项目,用于学习前端开发基础。用户可进行存款、取款操作,所有数据存储于浏览器内存中
|
前端开发
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
635 1
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
css3 svg制作404页面动画效果HTML源码
css3 svg制作404页面动画效果HTML源码
284 34
|
前端开发 JavaScript
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
588 14
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
|
人工智能 程序员 UED
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
769 21
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子