CSS旋转 动画效果

2023-4-26    前端达人

目录

动画基本使用

 动画序列

 动画常见属性

动画简写属性

大数据热点图案例

速度曲线之steps步长


动画基本使用

动画(animation)是CSS3中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常 用来实现复杂的动画效果。

相比较过渡,动画可以实现更多变化,更多控制,连续自动播放等效果。

制作动画分为两步: 1.先定义动画 2.再使用(调用)动画

1. 用keyframes 定义动画(类似定义类选择器)

@keyframes 动画名称 {

                   0%{

                                width:100px;

                                }

                        100%{

                                width:200px;

                }

}

动画序列 

0% 是动画的开始,100% 是动画的完成。这样的规则就是动画序列。

在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果。

动画是使元素从一种样式逐渐变化为另一种样式的效果。您可以改变任意多的样式任意的次数。

请用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%。

2. 元素使用动画

div {

width: 200px;

height: 200px;

background-color: aqua;

margin: 100px auto; /* 调用动画 */

animation-name: 动画名称;

/* 持续时间 */ animation-duration: 持续时间; } 

 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. /* 我们想页面一打开,一个盒子就从左边走到右边 */
  10. /* 1. 定义动画 */
  11. @keyframes move {
  12. /* 开始状态 */
  13. 0% {
  14. transform: translateX(0px);
  15. }
  16. /* 结束状态 */
  17. 100% {
  18. transform: translateX(1000px);
  19. }
  20. }
  21. div {
  22. width: 200px;
  23. height: 200px;
  24. background-color: pink;
  25. /* 2. 调用动画 */
  26. /* 动画名称 */
  27. animation-name: move;
  28. /* 持续时间 */
  29. animation-duration: 2s;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div></div>
  35. </body>
  36. </html>

 动画序列

 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. /* from to 等价于 0% 和 100% */
  10. /* @keyframes move {
  11. from {
  12. transform: translate(0, 0);
  13. }
  14. to {
  15. transform: translate(1000px, 0);
  16. }
  17. } */
  18. /* 动画序列 */
  19. /* 1. 可以做多个状态的变化 keyframe 关键帧 */
  20. /* 2. 里面的百分比要是整数 */
  21. /* 3. 里面的百分比就是 总的时间(我们这个案例10s)的划分 25% * 10 = 2.5s */
  22. @keyframes move {
  23. 0% {
  24. transform: translate(0, 0);
  25. }
  26. 25% {
  27. transform: translate(1000px, 0)
  28. }
  29. 50% {
  30. transform: translate(1000px, 500px);
  31. }
  32. 75% {
  33. transform: translate(0, 500px);
  34. }
  35. 100% {
  36. transform: translate(0, 0);
  37. }
  38. }
  39. div {
  40. width: 100px;
  41. height: 100px;
  42. background-color: pink;
  43. animation-name: move;
  44. animation-duration: 10s;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div>
  50. </div>
  51. </body>
  52. </html>

 动画常见属性

 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. @keyframes move {
  10. 0% {
  11. transform: translate(0, 0);
  12. }
  13. 100% {
  14. transform: translate(1000px, 0);
  15. }
  16. }
  17. div {
  18. width: 100px;
  19. height: 100px;
  20. background-color: pink;
  21. /* 动画名称 */
  22. animation-name: move;
  23. /* 持续时间 */
  24. /* animation-duration: 2s; */
  25. /* 运动曲线 */
  26. /* animation-timing-function: ease; */
  27. /* 何时开始 */
  28. animation-delay: 1s;
  29. /* 重复次数 iteration 重复的 conut 次数 infinite 无限 */
  30. /* animation-iteration-count: infinite; */
  31. /* 是否反方向播放 默认的是 normal 如果想要反方向 就写 alternate */
  32. /* animation-direction: alternate; */
  33. /* 动画结束后的状态 默认的是 backwards 回到起始状态 我们可以让他停留在结束状态 forwards */
  34. /* animation-fill-mode: forwards; */
  35. /* animation: name duration timing-function delay iteration-count direction fill-mode; */
  36. /* animation: move 2s linear 0s 1 alternate forwards; */
  37. /* 前面2个属性 name duration 一定要写 */
  38. /* animation: move 2s linear alternate forwards; */
  39. }
  40. div:hover {
  41. /* 鼠标经过div 让这个div 停止动画,鼠标离开就继续动画 */
  42. animation-play-state: paused;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div>
  48. </div>
  49. </body>
  50. </html>

动画简写属性

linear 匀速

animation:动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 动画起始或者结束的状态

animation: myfirst 5s linear 2s infinite alternate;

简写属性里面不包含 animation-play-state

暂停动画:animation-play-state: puased; 经常和鼠标经过等其他配合使用

想要动画走回来 ,而不是直接跳回来:animation-direction : alternate

盒子动画结束后,停在结束位置: animation-fill-mode : forwards 

大数据热点图案例

还没听。。


速度曲线之steps步长
 

animation-timing-function:规定动画的速度曲线,默认是“ease“

 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. overflow: hidden;
  11. font-size: 20px;
  12. width: 0;
  13. height: 30px;
  14. background-color: pink;
  15. /* 让我们的文字强制一行内显示 */
  16. white-space: nowrap;
  17. /* steps 就是分几步来完成我们的动画 有了steps 就不要在写 ease 或者linear 了 */
  18. animation: w 4s steps(10) forwards;
  19. }
  20. @keyframes w {
  21. 0% {
  22. width: 0;
  23. }
  24. 100% {
  25. width: 200px;
  26. }
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div>世纪佳缘我在这里等你</div>
  32. </body>
  33. </html>

分享本文至:

日历

链接

blogger

蓝蓝 http://www.lanlanwork.com

存档