当前位置:Gxlcms > css > css3D+动画的例子(附完整代码)

css3D+动画的例子(附完整代码)

时间:2021-07-01 10:21:17 帮助过:637人阅读

本篇文章给大家带来的内容是关于css3D+动画的例子(附完整代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

前言

最近玩了玩用css来构建3D效果,写了几个demo,所以博客总结一下。 在阅读这篇博客之前,请先自行了解一下css 3D的属性,例如:transform-style,transform-origin,transform, perspective。

写一个简单的立方体

1、我们先用css实现一个长方体,一个长方体有6个边,我们写6个li,并用一个ul包裹起来,根据我写3D动画的经验,最好有一个父元素来包裹

  1. <p class="parent">
  2. <ul>
  3. <li>1</li>
  4. <li>2</li>
  5. <li>3</li>
  6. <li>4</li>
  7. <li>5</li>
  8. <li>6</li>
  9. </ul>
  10. </p>

2、先给.parent设置宽高,并且给他设置视距和基点位置。

  1. .parent{
  2. width: 800px;
  3. height: 400px;
  4. border: 1px solid #000;
  5. margin: 0 auto;
  6. perspective: 2000px;
  7. perspective-origin: -40% -80%;
  8. background: #000;
  9. }

3、给ul设置宽高以及preserve-3d属性保留子元素3d转换,子元素li全部绝对定位

  1. ul{
  2. width: 50px;
  3. position: relative;
  4. margin: 100px auto;
  5. transform-style : preserve-3d;
  6. }
  7. li{
  8. width: 100px;
  9. height: 100px;
  10. background: rgba(255, 255, 0, 0.3);
  11. position: absolute;
  12. text-align: center;
  13. border: 3px solid greenyellow;
  14. }

效果如下图所示:

1462673038-5b70e99d5d54c_articlex.png

4、先写一个面,给他的背景设置 background: rgba(255, 255, 0, 0.3);

  1. li:nth-child(1){
  2. background: rgba(255, 255, 0, 0.3);
  3. transform: translateY(50px) rotateX(90deg);
  4. }

效果如下图所示:

3369746211-5b70e940019a9_articlex.png

5、我们写好了第一个面,然后我们在将其他6个面调整好,变成下图所示。关于rotate的旋转方向这里不解释,不懂的朋友可以自行查看其他文档。

  1. /*上面*/
  2. li:nth-child(1){
  3. transform: translateY(-50px) rotateX(90deg);
  4. }
  5. /*下面*/
  6. li:nth-child(2){
  7. transform: translateY(50px) rotateX(90deg);
  8. }
  9. /*左面*/
  10. li:nth-child(3){
  11. transform: translateX(-50px) rotateY(90deg);
  12. }
  13. /*右面*/
  14. li:nth-child(4){
  15. transform: translateX(50px) rotateY(90deg);
  16. }
  17. /*前面*/
  18. li:nth-child(5){
  19. transform: translateZ(50px);
  20. }
  21. /*后面*/
  22. li:nth-child(6){
  23. transform: translateZ(-50px);
  24. }

效果如下图所示:

1056950737-5b70eb243e4ca_articlex.png

下面是两种css3D+动画的效果

1、代码如下:

  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>书页2</title>
  8. <style>
  9. .container{
  10. width: 1000px;
  11. height: 650px;
  12. background: #000;
  13. perspective: 2000px;
  14. border: 1px solid transparent;
  15. overflow: hidden;
  16. margin: 0 auto;
  17. perspective-origin: 10% 20%;
  18. }
  19. .cube{
  20. width: 200px;
  21. height: 300px;
  22. transform-style: preserve-3d;
  23. margin:100px auto;
  24. position: relative;
  25. transform: rotateX(30deg);
  26. border-radius: 50%;
  27. padding: 60px;
  28. }
  29. .mian{
  30. width: 200px;
  31. height: 300px;
  32. background-image: url(1.jpg);
  33. background-position:400px 0;
  34. position: absolute;
  35. border: 1px solid #ccc;
  36. transition: 2s;
  37. }
  38. /* .mian1:hover{
  39. transform-origin: right;
  40. transform: rotateY(-60deg);
  41. } */
  42. .mian1{
  43. transform-origin: right;
  44. transform: translateX(-200px) rotateY(45deg);
  45. background-position: 0 0;
  46. }
  47. .mian3{
  48. transform-origin: left;
  49. transform: translateX(200px) rotateY(45deg);
  50. background-position: 200px 0;
  51. }
  52. .mian3:hover{
  53. transform: translateX(200px) rotateY(0deg);
  54. }
  55. .mian1:hover{
  56. transform: translateX(-200px) rotateY(0deg);
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <p class="container">
  62. <p class="cube">
  63. <p class="mian mian1"></p>
  64. <p class="mian mian2"></p>
  65. <p class="mian mian3"></p>
  66. </p>
  67. </p>
  68. </body>
  69. </html>

2、代码如下:

  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>立方体</title>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. }
  14. .parent{
  15. width: 1000px;
  16. margin: 0 auto;
  17. height: 600px;
  18. background: black;
  19. perspective: 5000px;
  20. perspective-origin: -40% -120%;
  21. border: 1px solid #000;
  22. }
  23. ul{
  24. width: 100px;
  25. height: 300px;
  26. position: relative;
  27. margin:100px auto;
  28. transform-style: preserve-3d;
  29. animation: zuan 3s linear infinite;
  30. border: 1px solid greenyellow;
  31. }
  32. li{
  33. width: 100px;
  34. height: 300px;
  35. background: rgba(0, 0, 0, 0.5);
  36. position: absolute;
  37. text-align: center;
  38. line-height: 100px;
  39. border: 3px solid greenyellow;
  40. }
  41. li:nth-child(1){
  42. transform: rotateY(30deg) translateZ(-200px);
  43. }
  44. li:nth-child(2){
  45. transform: rotateY(60deg) translateZ(-200px);
  46. background: rgba(255, 0, 0, 0.5);
  47. }
  48. li:nth-child(3){
  49. transform: rotateY(90deg) translateZ(-200px);
  50. }
  51. li:nth-child(4){
  52. transform: rotateY(120deg) translateZ(-200px);
  53. background: rgba(0, 0, 255, 0.5);
  54. }
  55. li:nth-child(5){
  56. transform: rotateY(150deg) translateZ(-200px);
  57. }
  58. li:nth-child(6){
  59. transform: rotateY(180deg) translateZ(-200px);
  60. background: rgba(255, 0, 255, 0.5);
  61. }
  62. li:nth-child(7){
  63. transform: rotateY(210deg) translateZ(-200px);
  64. }
  65. li:nth-child(8){
  66. transform: rotateY(240deg) translateZ(-200px);
  67. background: rgba(0, 255, 0, 0.5);
  68. }
  69. li:nth-child(9){
  70. transform: rotateY(270deg) translateZ(-200px);
  71. }
  72. li:nth-child(10){
  73. transform: rotateY(300deg) translateZ(-200px);
  74. background: rgba(0, 255, 255, 0.5);
  75. }
  76. li:nth-child(11){
  77. transform: rotateY(330deg) translateZ(-200px);
  78. }
  79. li:nth-child(12){
  80. transform: rotateY(360deg) translateZ(-200px);
  81. background: rgba(255, 255, 255, 0.5);
  82. }
  83. @keyframes zuan{
  84. 0%{
  85. transform: rotateY(0deg);
  86. }
  87. 100%{
  88. transform: rotateY(360deg);
  89. }
  90. }
  91. </style>
  92. </head>
  93. <body>
  94. <p class="parent">
  95. <ul>
  96. <li></li>
  97. <li></li>
  98. <li></li>
  99. <li></li>
  100. <li></li>
  101. <li></li>
  102. <li></li>
  103. <li></li>
  104. <li></li>
  105. <li></li>
  106. <li></li>
  107. <li></li>
  108. </ul>
  109. </p>
  110. </body>
  111. </html>

效果如下图:

2298908657-5b70eed2d024b_articlex.gif

相关推荐:

网页加载时样式效果css如何实现?(多种样式示例)

如何使用纯CSS实现一个微笑打坐的小和尚

字体加阴影效果怎么用css属性实现?(代码演示)

以上就是css3D+动画的例子(附完整代码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行