Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>立方体</title>
  6. <style>
  7. /*为什么两层div包裹各面后,旋转时不会出现畸变?*/
  8. .magic{
  9. position: absolute;/*子元素的绝对定位首先寻找已定位的父元素作参照。*/
  10. top: 50%;/*只有absolute才有top和left等值。 */
  11. left:50%;
  12. width: 300px;
  13. height: 300px;/*在这里设置了父元素的宽高,会继承给子元素。*/
  14. margin: -150px;/*注意这个margin的使用方法,他让元素居中。*/
  15. perspective:1000px;/*让里面的元素均可透视。*/
  16. }
  17. .magic .cube{
  18. position: absolute;/*使其能够继承父元素信息。*/
  19. width: 100%;
  20. height: 100%;
  21. transform-style:preserve-3d;/*保留子元素3d效果。*/
  22. /*perspective-origin:50% 35%;这个属性并不重要,为什么?*/
  23. animation:rotate 3s 12s linear infinite both;
  24. }
  25.  
  26. .magic .cube>div{
  27. position: absolute;
  28. width: 100%;
  29. height: 100%;
  30. box-sizing:border-box;/*将边框收入内容区,有什么用?*/
  31. border:1.2px solid black;/*这里指定了所有div的边框属性,选择器保障了特殊性,也无关继承*/
  32. transition:1s linear;
  33. text-align: center;
  34. font: 50px serif;
  35. /*line-height: 300px;为什么这里的line-height值无效?*/
  36. }
  37.  
  38. .magic .cube .front{/*注意这里的选择器使目标元素更特殊。*/
  39. animation:front 2s 10s linear both;/*注意这个linear属性的设置。*/
  40. line-height: 300px;
  41. }
  42.  
  43. .magic .cube .right{
  44. animation:right 2s 4s linear both;
  45. line-height: 300px;
  46. }
  47.  
  48. .magic .cube .left{
  49. animation:left 2s 6s linear both;
  50. line-height: 300px;
  51. }
  52.  
  53. .magic .cube .back{
  54. animation:back 2s 8s linear both;
  55. line-height: 300px;
  56. }
  57.  
  58. .magic .cube .top{
  59. animation:top 2s linear both;
  60. line-height: 300px;
  61. }
  62.  
  63. .magic .cube .bottom{
  64. animation:bottom 2s 2s linear both;
  65. line-height: 300px;
  66. }
  67.  
  68. .magic:hover .cube>div{/*注意这里选择器的用法。*/
  69. border-color:rgba(0,0,0,0);color:#fff;
  70. }
  71.  
  72. .magic:hover .cube .top{
  73. background:rgba(255,255,0,0.8);
  74. }
  75.  
  76. .magic:hover .cube .left{
  77. background:rgba(0,255,0,0.8);
  78. }
  79.  
  80. .magic:hover .cube .bottom{
  81. background:rgba(0,255,255,0.8);
  82. }
  83.  
  84. .magic:hover .cube .right{
  85. background:rgba(255,0,0,0.8);
  86. }
  87.  
  88. .magic:hover .cube .front{
  89. background:rgba(0,0,255,0.8);
  90. }
  91.  
  92. .magic:hover .cube .back{
  93. background:rgba(255,0,255,0.8);
  94. }
  95.  
  96. @keyframes top{
  97. 100%{transform:rotateX(90deg) translateZ(150px);/*注意这里两个动画没有任何符号连接!*/
  98. }
  99. }
  100.  
  101. @keyframes bottom{
  102. 100%{
  103. transform:rotateX(-90deg) translateZ(150px);
  104. }
  105. }
  106.  
  107. @keyframes right{
  108. 100%{
  109. transform:rotateY(90deg) translateZ(150px);
  110. }
  111. }
  112.  
  113. @keyframes left{
  114. 100%{
  115. transform:rotateY(-90deg) translateZ(150px);
  116. }
  117. }
  118.  
  119. @keyframes back{
  120. 100%{
  121. transform:rotateY(180deg) translateZ(150px);
  122. }
  123. }
  124.  
  125. @keyframes front{
  126. 100%{
  127. transform:translateZ(150px);
  128. }
  129. }
  130.  
  131. @keyframes rotate{
  132. 100%{
  133. transform:rotate3D(1,1,1,360deg);/*注意这个rotate值的设定。*/
  134. }
  135. }
  136. </style>
  137. </head>
  138. <body>
  139. <div class="magic">
  140. <div class="cube">
  141. <div class="front">front</div>
  142. <div class="right">right</div>
  143. <div class="back">back</div>
  144. <div class="left">left</div>
  145. <div class="top">top</div>
  146. <div class="bottom">bottom</div>
  147. </div>
  148. </div>
  149. </body>
  150. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement