【问题标题】:Rotate an element an a fixed axis将元素旋转到固定轴
【发布时间】:2021-06-11 13:38:08
【问题描述】:

有什么方法可以在 CSS/JS 或 GS​​AP 中实现类似的功能?

【问题讨论】:

  • 带有关键帧的css动画rotateZ()
  • 不要告诉我你在 google 上没有发现这个琐碎的动画吗?来吧..请稍微努力..

标签: javascript css animation


【解决方案1】:

一个简单的 CSS 示例

div {
  height: 100px;
  width: 100px;
  position: relative;
  background: blue;
  border-radius: 10px;
}

span {
  height: 10px;
  width: 50%;
  left: 50%;
  top: calc(50% - 5px);
  position: absolute;
  background: white;
  border-radius: 10em;
  animation-name: spin;
  animation-duration: 2000ms;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  transform-origin: left center;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<div>
  <span></span>
</div>

【讨论】:

    【解决方案2】:

    实现此目的的一种方法是创建一个使用transform: rotateZ 的旋转关键帧。确保将transform-origin 设置为left center,以便“针”div 围绕其左侧(而不是围绕其中心)旋转。

    .needle {
      width: 100px;
      height: 20px;
      background-color: lightgray;
      position: absolute;
      left: 50%;
      top: calc(50% - 10px);
      border-radius: 40px;
      transform-origin: left center;
      animation: rotation 2s linear infinite;
    }
    
    @keyframes rotation {
      from {
        transform: rotateZ(0);
      }
    
      to {
        transform: rotateZ(360deg);
      }
    }
    
    .container {
      width: 200px;
      height: 200px;
      background-color: darkblue;
      position: relative;
      border-radius: 10px;
    }
    <div class="container">
      <div class="needle"></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2011-08-22
      • 1970-01-01
      • 2020-04-07
      • 2018-02-20
      相关资源
      最近更新 更多