CSS旋轉 動畫效果

2023-4-26    前端達人

目錄

動畫基本使用

 動畫序列

 動畫常見屬性

動畫簡寫屬性

大數據熱點圖案例

速度曲線之steps步長


動畫基本使用

動畫(animation)是CSS3中具有顛覆性的特征之一,可通過設置多個節(jié)點來精確控制一個或一組動畫,常 用來實現復雜的動畫效果。

相比較過渡,動畫可以實現更多變化,更多控制,連續(xù)自動播放等效果。

制作動畫分為兩步: 1.先定義動畫 2.再使用(調用)動畫

1. 用keyframes 定義動畫(類似定義類選擇器)

@keyframes 動畫名稱 {

                   0%{

                                width:100px;

                                }

                        100%{

                                width:200px;

                }

}

動畫序列 

0% 是動畫的開始,100% 是動畫的完成。這樣的規(guī)則就是動畫序列。

在 @keyframes 中規(guī)定某項 CSS 樣式,就能創(chuàng)建由當前樣式逐漸改為新樣式的動畫效果。

動畫是使元素從一種樣式逐漸變化為另一種樣式的效果。您可以改變任意多的樣式任意的次數。

請用百分比來規(guī)定變化發(fā)生的時間,或用關鍵詞 "from" 和 "to",等同于 0% 和 100%。

2. 元素使用動畫

div {

width: 200px;

height: 200px;

background-color: aqua;

margin: 100px auto; /* 調用動畫 */

animation-name: 動畫名稱;

/* 持續(xù)時間 */ animation-duration: 持續(xù)時間; } 

 
  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. /* 開始狀態(tài) */
  13. 0% {
  14. transform: translateX(0px);
  15. }
  16. /* 結束狀態(tài) */
  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. /* 持續(xù)時間 */
  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. 可以做多個狀態(tài)的變化 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. /* 持續(xù)時間 */
  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. /* 動畫結束后的狀態(tài) 默認的是 backwards 回到起始狀態(tài) 我們可以讓他停留在結束狀態(tài) 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 停止動畫,鼠標離開就繼續(xù)動畫 */
  42. animation-play-state: paused;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div>
  48. </div>
  49. </body>
  50. </html>

動畫簡寫屬性

linear 勻速

animation:動畫名稱 持續(xù)時間 運動曲線 何時開始 播放次數 是否反方向 動畫起始或者結束的狀態(tài)

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:規(guī)定動畫的速度曲線,默認是“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>

分享本文至:

日歷

鏈接

個人資料

藍藍設計的小編 http://www.bouu.cn

存檔