代码之家  ›  专栏  ›  技术社区  ›  dev sandbox

在三维空间中垂直于父div旋转子div

  •  0
  • dev sandbox  · 技术社区  · 5 年前

    html {
      font-size: 0.7rem;
    }
    div div {
      top: 5rem;
      background-color: #444;
      transform-origin: top center;
    }
    
    .rotate_x {
      animation-name: rotate_x;
    }
    @keyframes rotate_x {
      0% {
        transform: rotateX( 0deg );
      }
      100% {
        transform: rotateX( 360deg );
      }
    }
    
    .rotate_y {
      animation-name: rotate_y;
    }
    @keyframes rotate_y {
      0% {
        transform: rotateY( 0deg );
      }
      100% {
        transform: rotateY( 180deg );
      }
    }
    <style>
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        animation-duration: 5s;
        animation-iteration-count: infinite;
        animation-timing-function: linear;
      }
      html, body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
        width: 100%;
      }
      body {
        perspective: 5rem;
      }
      div {
        position: relative;
        top: -1.5rem;
        width: 5rem;
        height: 5rem;
        background-color: #222;
        border-radius: 0.5rem;
      }
    </style>
    
    <div class='rotate_y'>
      <div class='rotate_x'>
      </div>
    </div>

    灰色的底部 div 看起来没有垂直于黑色的顶部旋转 如期而至。相反,底部的灰色div似乎在收缩,直到它有一个负值并被反转。所需的结果是底灰 像在铰链上一样向上旋转;在三维空间中形成“L”形 在它一路向上翻来翻去,在另一边做同样的事情之前。当父div旋转360度时。

    我怎样才能做底部 迪夫 创建一个'L'形状,因为它连接到它的父div?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Temani Afif    5 年前

    transform-style 把视角放大一点:

    html {
      font-size: 0.7rem;
    }
    div div {
      top: 5rem;
      background-color: #444;
      transform-origin: top center;
    }
    
    .rotate_x {
      animation-name: rotate_x;
    }
    @keyframes rotate_x {
      0% {
        transform: rotateX( 0deg );
      }
      100% {
        transform: rotateX( 360deg );
      }
    }
    
    .rotate_y {
      animation-name: rotate_y;
      transform-style:preserve-3d; /* HERE */
    }
    @keyframes rotate_y {
      0% {
        transform: rotateY( 0deg );
      }
      100% {
        transform: rotateY( 180deg );
      }
    }
    <style>
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        animation-duration: 5s;
        animation-iteration-count: infinite;
        animation-timing-function: linear;
      }
      html, body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
        width: 100%;
      }
      body {
        perspective: 15rem;
      }
      div {
        position: relative;
        top: -1.5rem;
        width: 5rem;
        height: 5rem;
        background-color: #222;
        border-radius: 0.5rem;
      }
    </style>
    
    <div class='rotate_y'>
      <div class='rotate_x'>
      </div>
    </div>