當前位置:
首頁 > 知識 > CSS3 3D transform實現旋轉木馬效果

CSS3 3D transform實現旋轉木馬效果

3D transform中有下面這幾個屬性:

  • rotateX(deg),rotateY(deg),rotateZ(deg)分別代表圍繞X軸,Y軸,Z軸旋轉。
  • translateX(px), translateY(px), translateZ(px)分別代表圍繞X軸,Y軸,Z軸位移。
  • transform-style: 使被轉換的子元素保留其 3D 轉換。
  • perspective:定義 3D 元素距視圖的距離,以像素計。當為元素定義 perspective 屬性時,其子元素會獲得透視效果,而不是元素本身。

旋轉木馬實現

1. 搭建舞台,加個視距 perspective: 600px;

  1. <style>
  2. .stage {
  3. height: 200px;
  4. width: 200px;
  5. margin: 0 auto;
  6. position: relative;
  7. margin-top: 300px;
  8. perspective: 600px;
  9. perspective-origin: 50%;
  10. }
  11. </style>
  12. <div class="stage"></div>

2. 添加容器,加個3D視圖聲明。

transform-style: preserve-3d;

3. 添加圖片,這裡用div代替圖片位置。我們讓所有圖片position:absolute,公用同一個中心點。

  1. .stage div.wrap {
  2. width: 100%;
  3. height: 100%;
  4. transform-style: preserve-3d;
  5. position: absolute;
  6. }
  7. .stage div.wrap div {
  8. width: 200px;
  9. height: 100%;
  10. background: #E00808;
  11. position: absolute;
  12. }
  13. .stage div.wrap div:nth-child(1) {
  14. background: #333;
  15. transform: rotateY(0deg);
  16. }
  17. .stage div.wrap div:nth-child(2) {
  18. background: #555;
  19. transform: rotateY(60deg);
  20. }
  21. .stage div.wrap div:nth-child(3) {
  22. background: #777;
  23. transform: rotateY(120deg);
  24. }
  25. .stage div.wrap div:nth-child(4) {
  26. background: #999;
  27. transform: rotateY(180deg);
  28. }
  29. .stage div.wrap div:nth-child(5) {
  30. background: #101010;
  31. transform: rotateY(240deg);
  32. }
  33. .stage div.wrap div:nth-child(6) {
  34. background: #001afe;
  35. transform: rotateY(300deg);
  36. }

4. 加上translateZ拉開空間

CSS3 3D transform實現旋轉木馬效果

上圖中紅色標註的r就是的demo頁面中圖片要translateZ的理想值(該值可以讓所有圖片無縫圍成一個圓)!

r的計算很簡單:

r = (div的一半寬度)/ Math.tan(角度 / 180 * Math.PI) 如上圖則 r = 64 / Math.tan(20 / 180 * Math.PI) = 175

因為我用了6個div,div寬度為200px,所以本例的 r 計算為

100 / Math.tan(30 / 180 * Math.PI) = 173

所以最終為:

  1. .stage div.wrap div {
  2. width: 200px;
  3. height: 100%;
  4. background: #E00808;
  5. position: absolute;
  6. }
  7. .stage div.wrap div:nth-child(1) {
  8. background: #333;
  9. transform: rotateY(0deg) translateZ(193px);
  10. }
  11. .stage div.wrap div:nth-child(2) {
  12. background: #555;
  13. transform: rotateY(60deg) translateZ(193px);
  14. }
  15. .stage div.wrap div:nth-child(3) {
  16. background: #777;
  17. transform: rotateY(120deg) translateZ(193px);
  18. }
  19. .stage div.wrap div:nth-child(4) {
  20. background: #999;
  21. transform: rotateY(180deg) translateZ(193px);
  22. }
  23. .stage div.wrap div:nth-child(5) {
  24. background: #101010;
  25. transform: rotateY(240deg) translateZ(193px);
  26. }
  27. .stage div.wrap div:nth-child(6) {
  28. background: #001afe;
  29. transform: rotateY(300deg) translateZ(193px);
  30. }

整合代碼

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>carousel</title>
  6. </head>
  7. <style>
  8. .stage {
  9. height: 200px;
  10. width: 200px;
  11. margin: 0 auto;
  12. position: relative;
  13. margin-top: 300px;
  14. perspective: 600px;
  15. perspective-origin: 50%;
  16. }
  17. .stage div.wrap {
  18. width: 100%;
  19. height: 100%;
  20. transform-style: preserve-3d;
  21. position: absolute;
  22. }
  23. .stage div.wrap div {
  24. width: 200px;
  25. height: 100%;
  26. background: #E00808;
  27. position: absolute;
  28. }
  29. .stage div.wrap div:nth-child(1) {
  30. background: #333;
  31. transform: rotateY(0deg) translateZ(193px);
  32. }
  33. .stage div.wrap div:nth-child(2) {
  34. background: #555;
  35. transform: rotateY(60deg) translateZ(193px);
  36. }
  37. .stage div.wrap div:nth-child(3) {
  38. background: #777;
  39. transform: rotateY(120deg) translateZ(193px);
  40. }
  41. .stage div.wrap div:nth-child(4) {
  42. background: #999;
  43. transform: rotateY(180deg) translateZ(193px);
  44. }
  45. .stage div.wrap div:nth-child(5) {
  46. background: #101010;
  47. transform: rotateY(240deg) translateZ(193px);
  48. }
  49. .stage div.wrap div:nth-child(6) {
  50. background: #001afe;
  51. transform: rotateY(300deg) translateZ(193px);
  52. }
  53. </style>
  54. <body>
  55. <div class="stage">
  56. <div class="wrap">
  57. <div></div>
  58. <div></div>
  59. <div></div>
  60. <div></div>
  61. <div></div>
  62. <div></div>
  63. </div>
  64. </div>
  65. </body>
  66. </html>

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

spring+mybatis 實現多數據源切換
Vue.js 2.0 漸進開發應用實踐

TAG:程序員小新人學習 |