【问题标题】:Rotate child div perpendicular to parent div in 3D space在 3D 空间中垂直于父 div 旋转子 div
【发布时间】:2020-12-04 06:41:01
【问题描述】:

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 旋转。相反,底部的灰色 div 似乎会缩小,直到它具有负值并被反转。想要的结果是底部灰色div 像铰链一样向上旋转;在 3D 空间中制作“L”形 在它一直向上并翻转以在另一侧做同样的事情之前。随着父 div 旋转 360 度。

如何使底部div 在与其父 div 连接时创建一个“L”形?

【问题讨论】:

    标签: html css 3d css-animations css-transforms


    【解决方案1】:

    你需要设置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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2019-09-02
      • 2016-06-21
      • 2017-08-13
      • 1970-01-01
      • 2015-04-18
      相关资源
      最近更新 更多