【问题标题】:CSS 3D animated wheel off centerCSS 3D 动画车轮偏离中心
【发布时间】:2020-09-07 22:58:24
【问题描述】:

我有一个在 x 轴上旋转的 3D css 轮/圆柱动画。我的问题是动画似乎在它的容器之外上下移动。下面的示例 GIF...

上面的代码可以在这里找到:https://jsfiddle.net/thelevicole/bkt0v1mc/

(function($) {

  const $wheel = $('.wheel .wheel__inner');
  const items = 28;
  const diameter = $wheel.height();
  const radius = diameter / 2;
  const angle = 360 / items;
  const circumference = Math.PI * diameter;
  const height = circumference / items;

  for (let i = 0; i < items; i++) {
    var transform = `rotateX(${ angle * i }deg) translateZ(${ radius }px)`;

    $('<div>', {
      class: 'wheel__segment'
    }).css({
      'transform': transform,
      'height': height,
    }).html(`<span>Item ${ i }</span>`).appendTo($wheel);
  }

})(jQuery);
*,
*:before,
*:after {
  box-sizing: border-box;
}

.wheel {
  perspective: 1000px;
  border: 1px solid #333;
  margin: 50px;
}

.wheel .wheel__inner {
  background-color: red;
  position: relative;
  width: 200px;
  height: 350px;
  margin: 0 auto;
  transform-style: preserve-3d;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  animation-name: spin;
  animation-duration: 6s;
}

.wheel .wheel__inner .wheel__segment {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 40px;
  position: absolute;
  top: 50%;
  background-color: #ccc;
}

.wheel .wheel__inner .wheel__segment:nth-child(even) {
  background-color: #ddd;
}

@-webkit-keyframes spin {
  0% {
    transform: rotateX(360deg);
  }
  50% {
    transform: rotateX(180deg);
  }
  100% {
    transform: rotateX(0deg);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wheel">
  <div class="wheel__inner">
  </div>
</div>

红色窗格是分段容器,这是带有 css 动画的元素。红色窗格保留在容器内,但是,这些线段垂直偏移,将它们推到容器外。

我可以通过将transform-origin: 50% 0; 添加到每个段来停止这种上下移动,但这会带来一个新问题。细分市场之间的差距!见下文...

以上代码:https://jsfiddle.net/thelevicole/bkt0v1mc/1/

( function( $ ) {

    const $wheel = $( '.wheel .wheel__inner' );
    const items = 28;
    const diameter = $wheel.height();
    const radius = diameter / 2;
    const angle = 360 / items;
    const circumference = Math.PI * diameter;
    const height = circumference / items;
    
    for ( let i = 0; i < items; i++ ) {
        var transform = `rotateX(${ angle * i }deg) translateZ(${ radius }px)`;

        $( '<div>', {
            class: 'wheel__segment'
        } ).css( {
            'transform': transform,
            'height': height,
        } ).html( `<span>Item ${ i }</span>` ).appendTo( $wheel );
    }
    
} )( jQuery );
*, *:before, *:after {
  box-sizing: border-box;
}

.wheel {
  perspective: 1000px;
  border: 1px solid #333;
  margin: 50px;
}
.wheel .wheel__inner {
  background-color: red;
  position: relative;
  width: 200px;
  height: 350px;
  margin: 0 auto;
  transform-style: preserve-3d;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  animation-name: spin;
  animation-duration: 6s;
}
.wheel .wheel__inner .wheel__segment {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 40px;
  position: absolute;
  top: 50%;
  background-color: #ccc;
  transform-origin: 50% 0;
}
.wheel .wheel__inner .wheel__segment:nth-child(even) {
  background-color: #ddd;
}

@-webkit-keyframes spin {
  0% {
    transform: rotateX(360deg);
  }
  50% {
    transform: rotateX(180deg);
  }
  100% {
    transform: rotateX(0deg);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wheel">
    <div class="wheel__inner">
    </div>
</div>

任何帮助都会很棒!

【问题讨论】:

    标签: javascript html css css-animations


    【解决方案1】:

    您可以通过调整旋转元素的transform-origin 来纠正此问题,如下所示:

    transform-origin: 50% calc(50% + height/2);
    

    ( function( $ ) {
    
        const $wheel = $( '.wheel .wheel__inner' );
        const items = 28;
        const diameter = $wheel.height();
        const radius = diameter / 2;
        const angle = 360 / items;
        const circumference = Math.PI * diameter;
        const height = circumference / items;
        
        for ( let i = 0; i < items; i++ ) {
            var transform = `rotateX(${ angle * i }deg) translateZ(${ radius }px)`;
    
            $( '<div>', {
                class: 'wheel__segment'
            } ).css( {
                'transform': transform,
                'height': height,
            } ).html( `<span>Item ${ i }</span>` ).appendTo( $wheel );
        }
      $wheel.css('transform-origin','50% calc(50% + '+height/2+'px)');
      $wheel.css('margin-top','-'+height+'px'); /* negative margin here to keep the element into the center */
        
    } )( jQuery );
    *, *:before, *:after {
      box-sizing: border-box;
    }
    
    .wheel {
      perspective: 1000px;
      border: 1px solid #333;
      margin: 50px;
    }
    .wheel .wheel__inner {
      background-color: red;
      position: relative;
      width: 200px;
      height: 350px;
      margin: 0 auto ; 
      transform-style: preserve-3d;
      animation-iteration-count: infinite;
      animation-timing-function: linear;
      animation-name: spin;
      animation-duration: 6s;
    }
    .wheel .wheel__inner .wheel__segment {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 40px;
      position: absolute;
      top: 50%;
      background-color: #ccc;
    }
    .wheel .wheel__inner .wheel__segment:nth-child(even) {
      background-color: #ddd;
    }
    
    @-webkit-keyframes spin {
      0% {
        transform: rotateX(360deg);
      }
      50% {
        transform: rotateX(180deg);
      }
      100% {
        transform: rotateX(0deg);
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="wheel">
        <div class="wheel__inner">
        </div>
    </div>

    【讨论】:

    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 2021-08-10
    • 2021-08-17
    • 2020-08-08
    • 2020-10-06
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多