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

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

[[347721]]

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

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

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

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

  1. @keyframes my 
  2. from {background: red;} 
  3. to {background: yellow;} 
  4.  
  5. @-moz-keyframes my /* Firefox */ 
  6. from {background: red;} 
  7. to {background: yellow;} 
  8.  
  9. @-webkit-keyframes my /* Safari 和 Chrome */ 
  10. from {background: red;} 
  11. to {background: yellow;} 
  12.  
  13. @-o-keyframes my /* Opera */ 
  14. from {background: red;} 
  15. to {background: yellow;} 

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

  1. @keyframes my 
  2. 0%   {background: red;} 
  3. 25%  {background: yellow;} 
  4. 50%  {background: blue;} 
  5. 100% {background: green;} 
  6.  
  7. @-moz-keyframes my /* Firefox */ 
  8. 0%   {background: red;} 
  9. 25%  {background: yellow;} 
  10. 50%  {background: blue;} 
  11. 100% {background: green;} 
  12.  
  13. @-webkit-keyframes my /* Safari 和 Chrome */ 
  14. 0%   {background: red;} 
  15. 25%  {background: yellow;} 
  16. 50%  {background: blue;} 
  17. 100% {background: green;} 
  18.  
  19. @-o-keyframes my /* Opera */ 
  20. 0%   {background: red;} 
  21. 25%  {background: yellow;} 
  22. 50%  {background: blue;} 
  23. 100% {background: green;} 

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

2).animation启动动画效果

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

参考文档: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

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前端

2009-04-08 10:51:59

Windows Emb

2022-03-30 14:34:21

鸿蒙HarmonyOScss

2011-07-29 14:55:25

iPhone开发 动画过渡

2015-08-03 11:42:27

Swift汉堡式过度动画

2022-12-28 08:16:30

CSS新规范样式

2010-09-13 13:44:35

CSS表格CSS表单

2024-03-22 12:22:50

Vue前端

2009-03-06 14:39:20

LinuxUbuntuUsplash

2023-07-14 07:52:37

CSS优先级Design

2010-08-31 09:39:17

CSS样式表
点赞
收藏

51CTO技术栈公众号