backface-visibility: hidden; 3D 翻转时,看不到背面

CSS3 中的transform-origin属性用于设置旋转元素的基点位置

  1. transform-origin: x-axis y-axis z-axis;
  2. /*默认值*/
  3. transform-origin: 50% 50% 0;

transform-style

CSS 属性 transform-style 设置元素的子元素是位于 3D 空间中还是平面中。
如果选择平面,元素的子元素将不会有 3D 的遮挡关系。
由于这个属性不会被继承,因此必须为元素的所有非叶子子元素设置它。

  1. /* Keyword values */
  2. transform-style: flat;
  3. transform-style: preserve-3d;
  4. /* Global values */
  5. transform-style: inherit;
  6. transform-style: initial;
  7. transform-style: unset;

image.png
image.png

元素按中心旋转

image.png

  1. <div class="avatar"></div>
  1. .avatar {
  2. position: fixed;
  3. left: 200px;
  4. top: 200px;
  5. width: 200px;
  6. height: 200px;
  7. background-color: blueviolet;
  8. }
  9. .avatar {
  10. transform: rotate(45deg); // 按中心旋转45度
  11. }

元素按圆轨迹运动

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>css3</title>
  6. </head>
  7. <body>
  8. <style>
  9. .avatar {
  10. position: fixed;
  11. left: 200px;
  12. top: 200px;
  13. width: 400px;
  14. height: 400px;
  15. background-color: blueviolet;
  16. border-radius: 100%;
  17. }
  18. @keyframes spin {
  19. /* CSS3 角度单位 turn(圈) */
  20. to{transform: rotate(1turn);}
  21. }
  22. .ani {
  23. animation: spin 3s infinite linear;
  24. transform-origin: 50% 195px;
  25. }
  26. .img {
  27. position: absolute;
  28. left: 180px;
  29. top: 5px;
  30. border-radius: 100%;
  31. width: 40px;
  32. height: 40px;
  33. background-color: blue;
  34. }
  35. </style>
  36. <div class="avatar">
  37. <div class="img ani"></div>
  38. </div>
  39. </body>
  40. </html>

跟着椭圆移动 position: absolute;

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>css3</title>
  6. </head>
  7. <body>
  8. <style>
  9. .avatar {
  10. position: fixed;
  11. left: 200px;
  12. top: 200px;
  13. width: 300px;
  14. height: 200px;
  15. }
  16. .img {
  17. position: absolute;
  18. border-radius: 100%;
  19. width: 40px;
  20. height: 40px;
  21. background-color: blue;
  22. }
  23. .item {
  24. position: relative;
  25. width: 100%;
  26. height: 100%;
  27. background-color: rgba(138, 43, 226, 1);
  28. border-radius: 100%;
  29. }
  30. </style>
  31. <div class="avatar">
  32. <div class="item"></div>
  33. <div class="img" id="img"></div>
  34. </div>
  35. <script>
  36. // a 长半径, b 短半径,angle 角度,cx cy 圆心
  37. function getCPoint(a, b, angle, cx = 0, cy = 0) {
  38. index = angle
  39. let x = a * Math.cos(Math.PI * 2 * index / 360)
  40. let y = b * Math.sin(Math.PI * 2 * index / 360)
  41. x = (x + cx);
  42. y = (y + cy);
  43. return {
  44. x, y
  45. }
  46. }
  47. const avatar = document.querySelector('.avatar')
  48. const div = document.getElementById('img')
  49. let step = 360;
  50. const loop = function (time, bool) {
  51. if (step > 0 || bool) {
  52. step--;
  53. const posi = getCPoint(150, 100, step, 150 - 20, 100 - 20)
  54. div.style.left = posi.x + 'px';
  55. div.style.top = posi.y + 'px';
  56. setTimeout(() => {
  57. loop(time, bool);
  58. }, time || 100);
  59. }
  60. }
  61. loop(10, true);
  62. </script>
  63. </body>
  64. </html>

跟着椭圆排列,使用 transform: translate(x, y)

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>css3</title>
  6. </head>
  7. <body>
  8. <style>
  9. .avatar {
  10. display: flex;
  11. position: fixed;
  12. left: 200px;
  13. top: 200px;
  14. width: 300px;
  15. height: 200px;
  16. }
  17. .img {
  18. flex: none;
  19. z-index: 2;
  20. border-radius: 100%;
  21. width: 40px;
  22. height: 40px;
  23. background-color: blue;
  24. display: flex;
  25. justify-content: center;
  26. align-items: center;
  27. color: #fff;
  28. }
  29. .item {
  30. position: absolute;
  31. width: 100%;
  32. height: 100%;
  33. background-color: rgba(138, 43, 226, 1);
  34. border-radius: 100%;
  35. }
  36. </style>
  37. <div class="avatar">
  38. <div class="item"></div>
  39. <div class="img">1</div>
  40. <div class="img">2</div>
  41. <div class="img">3</div>
  42. <div class="img">4</div>
  43. <div class="img">5</div>
  44. <div class="img">6</div>
  45. <div class="img">7</div>
  46. <div class="img">8</div>
  47. <div class="img">9</div>
  48. <div class="img">10</div>
  49. <div class="img">11</div>
  50. <div class="img">12</div>
  51. </div>
  52. <script>
  53. // a 长半径, b 短半径,angle 角度,cx cy 圆心
  54. function getCPoint(a, b, angle, cx = 0, cy = 0) {
  55. index = angle
  56. let x = a * Math.cos(Math.PI * 2 * index / 360)
  57. let y = b * Math.sin(Math.PI * 2 * index / 360)
  58. x = (x + cx);
  59. y = (y + cy);
  60. return {
  61. x, y
  62. }
  63. }
  64. const avatar = document.querySelector('.avatar')
  65. let divs = document.querySelectorAll('.img')
  66. divs = Array.prototype.slice.call(divs)
  67. console.log(divs.forEach, divs.length)
  68. for (let i = 0; i < divs.length; i+=1) {
  69. const item = divs[i];
  70. console.log(item.offsetLeft, item.offsetTop)
  71. const {x, y} = getCPoint(150, 100, (360 / divs.length) * i, 130, 80);
  72. console.log(x, y)
  73. // item.style.left = x + 'px';
  74. // item.style.top = y + 'px';
  75. item.style.transform = `translate(${x - item.offsetLeft}px, ${y - item.offsetTop}px)`
  76. }
  77. </script>
  78. </body>
  79. </html>