CSS(Cascading Style Sheets)样式更改——过渡、动画

开发 前端
这篇文章我们来介绍下CSS样式更改中的过渡、动画基础用法。

[[347721]]

前言
这篇文章我们来介绍下CSS样式更改中的过渡、动画基础用法。

1.过渡
元素从一种样式逐渐改变为另一种的样式 。

div 

transition: width 1s; 
-moz-transition: width 1s;  /* Firefox 4 */ 
-webkit-transition: width 1s;  /* Safari 和 Chrome */ 
-o-transition: width 1s;  /* Opera */ 

transition-property:应用过渡的Css属性的名称 比如宽度width 
transition-duration:过渡效果花费的时间   比如1s 
transition-timing-function:渡效果的时间曲线 如下所示: 
linear 匀速 
ease 先慢后快 
ease-in 慢速开始 
ease-out 慢速结束 
ease-in-out 慢速开始和结束 
cubic-bezier(n,n,n,n) 在cubic-bezie 函数中定义自己的值,可能的值是0至1之间的数值 
transition-delay:过渡效果何时开始 如1s 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

2.动画 Animation
1).首先定义@keyframes 规则

@keyframes my 

from {background: red;} 
to {background: yellow;} 

 
@-moz-keyframes my /* Firefox */ 

from {background: red;} 
to {background: yellow;} 

 
@-webkit-keyframes my /* Safari 和 Chrome */ 

from {background: red;} 
to {background: yellow;} 

 
@-o-keyframes my /* Opera */ 

from {background: red;} 
to {background: yellow;} 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

为了丰富元素的变化过程,你可以把from to改为百分比的样子:

@keyframes my 

0%   {background: red;} 
25%  {background: yellow;} 
50%  {background: blue;} 
100% {background: green;} 

 
@-moz-keyframes my /* Firefox */ 

0%   {background: red;} 
25%  {background: yellow;} 
50%  {background: blue;} 
100% {background: green;} 

 
@-webkit-keyframes my /* Safari 和 Chrome */ 

0%   {background: red;} 
25%  {background: yellow;} 
50%  {background: blue;} 
100% {background: green;} 

 
@-o-keyframes my /* Opera */ 

0%   {background: red;} 
25%  {background: yellow;} 
50%  {background: blue;} 
100% {background: green;} 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

定义好了,接下来我们就可以启动我们的动画了。

2).animation启动动画效果

div 

animation-name: my; 
animation-duration: 5s; 
animation-timing-function: linear; 
animation-delay: 2s; 
animation-iteration-count: infinite; 
animation-direction: alternate; 
animation-play-state: running; 
/* Firefox: */ 
-moz-animation-name: my; 
-moz-animation-duration: 5s; 
-moz-animation-timing-function: linear; 
-moz-animation-delay: 2s; 
-moz-animation-iteration-count: infinite; 
-moz-animation-direction: alternate; 
-moz-animation-play-state: running; 
/* Safari 和 Chrome: */ 
-webkit-animation-name: my; 
-webkit-animation-duration: 5s; 
-webkit-animation-timing-function: linear; 
-webkit-animation-delay: 2s; 
-webkit-animation-iteration-count: infinite; 
-webkit-animation-direction: alternate; 
-webkit-animation-play-state: running; 
/* Opera: */ 
-o-animation-name: my; 
-o-animation-duration: 5s; 
-o-animation-timing-function: linear; 
-o-animation-delay: 2s; 
-o-animation-iteration-count: infinite; 
-o-animation-direction: alternate; 
-o-animation-play-state: running; 

 
animation-name                   选择器的 keyframes 的名称 
animation-duration               动画所花费的时间 
animation-timing-function        匀速播放动画 
animation-delay           动画过多久开始 
animation-iteration-count        播放动画次数 
animation-direction       是否在下一周期逆向地播放 normal 正常播放  alternate 轮流反向播放 
animation-play-state             暂停动画  paused 动画已暂停  running 动画正在播放 
animation-fill-mode 
none         不填充 
forwards     当动画完成后,保持最后一个属性值 
backwards     在animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值 
both        向前和向后填充模式都被应用。 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.

参考文档:W3C官方文档(CSS篇)

总结
这篇文章主要介绍了CSS样式更改篇中的过度和动漫基础知识,希望让大家对CSS样式更改有个简单的认识和了解。

责任编辑:姜华 来源: IT共享之家
相关推荐

2020-10-23 08:51:55

CSS

2020-10-26 13:40:00

CascadingSt

2010-09-14 15:04:42

list-styleCSS

2024-09-23 09:20:02

calc-sizeCSS前端

2023-02-06 09:31:17

CSSJS 动态

2024-03-28 09:11:24

CSS3TransitionCSS属性

2021-05-21 07:41:15

Vue 过渡动画

2023-04-14 16:45:21

CSS前端CSS3

2013-01-30 15:59:29

adobeCSS3HTML5

2017-07-20 11:11:39

前端CSS书写规范

2023-11-20 09:27:28

CSS前端

2022-03-30 14:34:21

鸿蒙HarmonyOScss

2009-04-08 10:51:59

Windows Emb

2022-12-28 08:16:30

CSS新规范样式

2010-09-13 13:44:35

CSS表格CSS表单

2015-08-03 11:42:27

Swift汉堡式过度动画

2011-07-29 14:55:25

iPhone开发 动画过渡

2024-03-22 12:22:50

Vue前端

2025-01-10 08:46:09

2023-07-14 07:52:37

CSS优先级Design
点赞
收藏

51CTO技术栈公众号