TranslateX TranslateY

  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. <title>Document</title>
  7. <style type="text/css">
  8. *{
  9. margin: 0px;
  10. padding: 0px;
  11. }
  12. .div1 {
  13. width: 100px;
  14. height: 100px;
  15. background-color: aquamarine;
  16. transform: translateY(10px);
  17. }
  18. .behind {
  19. width: 300px;
  20. height: 300px;
  21. background-color: blueviolet;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="div1"></div>
  27. <div class="behind"></div>
  28. </body>
  29. </html>

image.png
translateY(10px), 上面的div沿着Y轴的方向,向下移动10px,
translateX(10px), 上面的div沿着X轴的方向,向右移动10px
translate(10px, 10px),上面的div分别沿着X,Y轴正方向各移动10px
translate 不会对其他布局产生影响。
**

实例

css translate 实例

  1. <h1>Css3 Transform</h1>
  2. <!-- Rotate-->
  3. <div class="card">
  4. <div class="box rotate">
  5. <div class="fill"></div>
  6. </div>
  7. <p>rotate(45deg) </p>
  8. </div>
  9. <div class="card">
  10. <div class="box rotateX">
  11. <div class="fill"></div>
  12. </div>
  13. <p>rotateX(45deg)</p>
  14. </div>
  15. <div class="card">
  16. <div class="box rotateY">
  17. <div class="fill"></div>
  18. </div>
  19. <p>rotateY(45deg)</p>
  20. </div>
  21. <div class="card">
  22. <div class="box rotateZ">
  23. <div class="fill"></div>
  24. </div>
  25. <p>rotateZ(45deg) </p>
  26. </div>
  27. <!-- scale-->
  28. <div class="card">
  29. <div class="box scale">
  30. <div class="fill"></div>
  31. </div>
  32. <p>scale(2)</p>
  33. </div>
  34. <div class="card">
  35. <div class="box scaleX">
  36. <div class="fill"></div>
  37. </div>
  38. <p>scaleX(2) </p>
  39. </div>
  40. <div class="card">
  41. <div class="box scaleY">
  42. <div class="fill"></div>
  43. </div>
  44. <p>scaleY(2) </p>
  45. </div>
  46. <!-- skew-->
  47. <div class="card">
  48. <div class="box skew">
  49. <div class="fill"></div>
  50. </div>
  51. <p>skew(45deg, 45deg) </p>
  52. </div>
  53. <div class="card">
  54. <div class="box skewX">
  55. <div class="fill"></div>
  56. </div>
  57. <p>skewX(45deg)</p>
  58. </div>
  59. <div class="card">
  60. <div class="box skewY">
  61. <div class="fill"></div>
  62. </div>
  63. <p>skewY(45deg)</p>
  64. </div>
  65. <!-- translate-->
  66. <div class="card">
  67. <div class="box translate">
  68. <div class="fill"></div>
  69. </div>
  70. <p>translate(45px) </p>
  71. </div>
  72. <div class="card">
  73. <div class="box translateX">
  74. <div class="fill"></div>
  75. </div>
  76. <p>translateX(45px)</p>
  77. </div>
  78. <div class="card">
  79. <div class="box translateY">
  80. <div class="fill"></div>
  81. </div>
  82. <p>translateY(45px)</p>
  83. </div>
  84. <div class="card">
  85. <div class="box matrix">
  86. <div class="fill"></div>
  87. </div>
  88. <p> matrix(2, 2, 0, 2, 45, 0)</p>
  89. </div>
  90. <h4>Perspective : 100</h4>
  91. <div class="perspective-100">
  92. <div class="card">
  93. <div class="box rotateX">
  94. <div class="fill"></div>
  95. </div>
  96. <p>rotateX(90deg)</p>
  97. </div>
  98. <div class="card">
  99. <div class="box rotateY">
  100. <div class="fill"></div>
  101. </div>
  102. <p>rotateY(45deg)</p>
  103. </div>
  104. </div>
  105. <h4>Perspective : 200</h4>
  106. <div class="perspective-200">
  107. <div class="card">
  108. <div class="box rotateX">
  109. <div class="fill"></div>
  110. </div>
  111. <p>rotateX(90deg)</p>
  112. </div>
  113. <div class="card">
  114. <div class="box rotateY">
  115. <div class="fill"></div>
  116. </div>
  117. <p>rotateY(45deg)</p>
  118. </div>
  119. </div>
  120. <!-- transform origin-->
  121. <h2>Transform origin</h2>
  122. <div class="card">
  123. <div class="box rotate">
  124. <div class="fill to-100-0-0"></div>
  125. </div>
  126. <p>transform-origin : 100% 0 0 <br/>rotate(45deg)</p>
  127. </div>
  128. <div class="card">
  129. <div class="box rotate">
  130. <div class="fill to-0-100-0"></div>
  131. </div>
  132. <p>transform-origin : 0 100% 0<br/>rotate(45deg)</p>
  133. </div>
  134. <div class="card perspective-200">
  135. <div class="box rotateX">
  136. <div class="fill to-0-100-0"></div>
  137. </div>
  138. <p>transform-origin : 0 100% 0<br/>rotateX(45deg)</p>
  139. </div>
  140. <div class="card perspective-200">
  141. <div class="box rotateX">
  142. <div class="fill to-100-0-0"></div>
  143. </div>
  144. <p>transform-origin : 100% 0 0<br/>rotateX(45deg)</p>
  145. </div>
  146. <div class="card perspective-200">
  147. <div class="box rotateY">
  148. <div class="fill to-0-100-0"></div>
  149. </div>
  150. <p>transform-origin : 0 100% 0 <br/>rotateY(45deg)</p>
  151. </div>
  152. <div class="card perspective-200">
  153. <div class="box rotateY">
  154. <div class="fill to-100-0-0"></div>
  155. </div>
  156. <p>transform-origin : 100% 0 0<br/>rotateY(45deg)</p>
  157. </div>
  158. <div class="card">
  159. <div class="box scale">
  160. <div class="fill to-100-0-0"></div>
  161. </div>
  162. <p>transform-origin : 100% 0 0<br/>scale(2)</p>
  163. </div>
  164. <div class="card">
  165. <div class="box scale">
  166. <div class="fill to-0-100-0"></div>
  167. </div>
  168. <p>transform-origin : 0 100% 0<br/>scale(2)</p>
  169. </div>
  170. <div class="card">
  171. <div class="box scaleX">
  172. <div class="fill to-100-0-0"></div>
  173. </div>
  174. <p>transform-origin : 100% 0 0<br/>scaleX(2)</p>
  175. </div>
  176. <div class="card">
  177. <div class="box scaleX">
  178. <div class="fill to-0-100-0"></div>
  179. </div>
  180. <p>transform-origin : 0 100% 0<br/>scaleX(2)</p>
  181. </div>
  182. <div class="card">
  183. <div class="box scaleY">
  184. <div class="fill to-100-0-0"></div>
  185. </div>
  186. <p>transform-origin : 100% 0 0<br/>scaleY(2)</p>
  187. </div>
  188. <div class="card">
  189. <div class="box scaleY">
  190. <div class="fill to-0-100-0"></div>
  191. </div>
  192. <p>transform-origin : 0 100% 0<br/>scaleY(2)</p>
  193. </div>
  1. *, *:after, *:before {
  2. box-sizing: border-box;
  3. }
  4. body {
  5. background: #F5F3F4;
  6. margin: 0;
  7. padding: 10px;
  8. font-family: 'Open Sans', sans-serif;
  9. text-align: center;
  10. }
  11. h1 {
  12. color: #4c4c4c;
  13. font-weight: 600;
  14. border-bottom: 1px solid #ccc;
  15. }
  16. h2, h4 {
  17. font-weight: 400;
  18. color: #4d4d4d;
  19. }
  20. .card {
  21. display: inline-block;
  22. margin: 10px;
  23. background: #fff;
  24. padding: 15px;
  25. min-width: 200px;
  26. box-shadow: 0 3px 5px #ddd;
  27. color: #555;
  28. }
  29. .card .box {
  30. width: 100px;
  31. height: 100px;
  32. margin: auto;
  33. background: #ddd;
  34. cursor: pointer;
  35. box-shadow: 0 0 5px #ccc inset;
  36. }
  37. .card .box .fill {
  38. width: 100px;
  39. height: 100px;
  40. position: relative;
  41. background: #03A9F4;
  42. opacity: .5;
  43. box-shadow: 0 0 5px #ccc;
  44. -webkit-transition: 0.3s;
  45. transition: 0.3s;
  46. }
  47. .card p {
  48. margin: 25px 0 0;
  49. }
  50. .rotate:hover .fill {
  51. -webkit-transform: rotate(45deg);
  52. transform: rotate(45deg);
  53. }
  54. .rotateX:hover .fill {
  55. -webkit-transform: rotateX(45deg);
  56. transform: rotateX(45deg);
  57. }
  58. .rotateY:hover .fill {
  59. -webkit-transform: rotateY(45deg);
  60. transform: rotateY(45deg);
  61. }
  62. .rotateZ:hover .fill {
  63. -webkit-transform: rotate(45deg);
  64. transform: rotate(45deg);
  65. }
  66. .scale:hover .fill {
  67. -webkit-transform: scale(2, 2);
  68. transform: scale(2, 2);
  69. }
  70. .scaleX:hover .fill {
  71. -webkit-transform: scaleX(2);
  72. transform: scaleX(2);
  73. }
  74. .scaleY:hover .fill {
  75. -webkit-transform: scaleY(2);
  76. transform: scaleY(2);
  77. }
  78. .skew:hover .fill {
  79. -webkit-transform: skew(45deg, 45deg);
  80. transform: skew(45deg, 45deg);
  81. }
  82. .skewX:hover .fill {
  83. -webkit-transform: skewX(45deg);
  84. transform: skewX(45deg);
  85. }
  86. .skewY:hover .fill {
  87. -webkit-transform: skewY(45deg);
  88. transform: skewY(45deg);
  89. }
  90. .translate:hover .fill {
  91. -webkit-transform: translate(45px, 1em);
  92. transform: translate(45px, 1em);
  93. }
  94. .translateX:hover .fill {
  95. -webkit-transform: translateX(45px);
  96. transform: translateX(45px);
  97. }
  98. .translateY:hover .fill {
  99. -webkit-transform: translateY(45px);
  100. transform: translateY(45px);
  101. }
  102. .matrix:hover .fill {
  103. -webkit-transform: matrix(2, 2, 0, 2, 45, 0);
  104. transform: matrix(2, 2, 0, 2, 45, 0);
  105. }
  106. .perspective-100 .box {
  107. -webkit-perspective: 100px;
  108. perspective: 100px;
  109. }
  110. .perspective-200 .box {
  111. -webkit-perspective: 200px;
  112. perspective: 200px;
  113. }
  114. .to-100-0-0 {
  115. -webkit-transform-origin: 100% 0 0;
  116. transform-origin: 100% 0 0;
  117. }
  118. .to-0-100-0 {
  119. -webkit-transform-origin: 0 100% 0;
  120. transform-origin: 0 100% 0;
  121. }