【问题标题】:Animation - unexpected position change动画 - 意外的位置变化
【发布时间】:2018-01-27 22:55:18
【问题描述】:

我的代码有问题。 我想让一个 div 旋转,但是当 div 旋转时,位置与以前不同。

.rotateWindow {
   position: absolute;
  text-align: center;
  margin: 0 auto;
  width: 150px;
  height: 150px;
  left:50%;
  top:50%;
  transform: translate(-50%, -50%);
  line-height: 32px;
  border: solid 7px;
  border-top: #f8f8ff;
  border-radius: 50%;
  font-family: 'Lobster', cursive;
  color: #f8f8ff;
  font-size: 18px;
  z-index: -1;
      -webkit-animation-name: spin; 
    -webkit-animation-iteration-count: infinite; 
    -webkit-animation-timing-function: ease-in-out;
    -webkit-animation-duration: 6s; 

}

@-webkit-keyframes spin {  
from {  
    -webkit-transform: rotate(0deg);  
}  
to {  
    -webkit-transform: rotate(360deg);  
    } 
}
}

没有动画,它位于页面中心,在我添加动画后,它位于其主要位置下方。 我也尝试了一个简单的 jquery 代码,但还是一样。

$(function() {
    var $elie = $('.rotateWindow');
    rotate(0);
    function rotate(degree) {

          // For webkit browsers: e.g. Chrome
        $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
          // For Mozilla browser: e.g. Firefox
        $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});

          // Animate rotation with a recursive call
        setTimeout(function() { rotate(++degree); },5);
    }
});

我该如何解决这个问题以及为什么会出错? 谢谢你的建议。

【问题讨论】:

    标签: jquery css animation css-animations center


    【解决方案1】:

    您需要使用transform-origin: 50% 50%,以便您的元素围绕其中心点旋转。

    【讨论】:

    • 它现在就在做,但不在它应该做的地方。它应该在页面的中心进行,但事实并非如此。如果没有动画,它会在中心占据一席之地,但是当我添加动画时,它会在中心下方并且在右侧一点
    • 和 transform-origin 没有区别
    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    相关资源
    最近更新 更多