
1. 关键帧动画
1.1 animation 复合属性
animation 用于创建动画效果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| animation: name duration timing-function delay iteration-count direction fill-mode; animation: 名称 持续时间 过渡类型 [延迟时间] [循环次数] [循环中是否反向运动] [填充模式]; eg: animation: myrun1 2s linear infinite; animation: myrun1 2s linear 1s infinite; animation: myrun1 0.5s linear forwards; animation: run 0.5s linear forwards reverse;
@keyframes myrun1 { from { 样式 } to { 样式 } }
|
特别说明:
- animation-fill-mode, 填充模式
- none,默认,不保留动画
- forwards,保留最后一帧,即最后画面
- backwards,保留第一帧,即初始画面
1.2 animation-xxx 单一属性




示例:基本用法
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div{ width: 200px; height: 200px; background: red; animation: myrun1 2s linear 1s infinite; } @keyframes myrun1 { from { width: 200px; height: 200px; background: red; } to { width: 400px; height: 600px; background: green; } } @keyframes myrun2 { 0% { width: 200px; height: 200px; background: red; } 20% { width: 200px; height: 400px; background: yellow; } 50% { width: 400px; height: 400px; background: blue; } 72% { width: 400px; height: 600px; background: gray; } to { width: 400px; height: 600px; background: green; } } </style> </head> <body> <div></div> </body> </html>
|

效果:


效果:
.gif)
案例:抽屉效果
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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; } html,body{ height: 100%; } div{ height: 100%; width: 200px; background: red; position: fixed; left: 0; top: 0;
transform: translateX(-100%); animation: run 0.5s linear forwards;
}
@keyframes run { from{ transform: translateX(-100%); } to{ transform: translateX(0); } } </style> </head> <body> <div></div> </body> </html>
|
效果:

案例:轮播图效果
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; } .swiper-container{ width: 768px; height: 240px; margin: 10px auto; overflow: hidden; }
.swiper-container img{ width: 768px; height: 240px; }
.swiper-slide{ float: left; }
.swiper-wrapper{ width: 9999px; animation: swiperrun 10s infinite; }
@keyframes swiperrun { 0%{ transform: translateX(0); } 20%{ transform: translateX(0); } 40%{ transform: translateX(-768px); } 60%{ transform: translateX(-768px); } 80%{ transform: translateX(-1536px); } 100%{ transform: translateX(-1536px); } }
.swiper-wrapper:hover{ animation-play-state: paused; } </style> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"> <img src="./img/1.jpg" alt=""> </div> <div class="swiper-slide"> <img src="./img/2.jpg" alt=""> </div> <div class="swiper-slide"> <img src="./img/3.jpg" alt=""> </div> </div> </div> </body> </html>
|
效果:

案例:逐帧动画效果