平滑 3D 革命的 CSS3 动画功能?

标签 css rotation css-animations

我有这个pen它试图模拟围绕某物旋转的对象。这有效,但并不顺利。旋转时它会在左右边缘停顿。

我认为它与animation-timing-function有关但无法使用任何内置函数(如 ease-in-out)获得所需的结果或 linear或自定义 cubic-bezier功能。

如何让动画感觉流畅?如果有更好的方法可以完成这样的事情,请随时告诉我。

.overlay {
  background-image: -webkit-repeating-linear-gradient(0deg, transparent, transparent 1%, rgb(255, 255, 255) 2%, rgb(255, 255, 255) 2%);
  background-image: repeating-linear-gradient(90deg, transparent, transparent 1%, rgb(255, 255, 255) 2%, rgb(255, 255, 255) 2%);
  height: 200px;
  position: relative;
  width: 40%;
  margin: auto;
}
.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #888;
  position: absolute;
  z-index: -1;
  left: 0;
  display: inline-block;
}
.move {
  -webkit-animation: moveAndGlow 2s infinite ease-in-out;
  animation: moveAndGlow 2s infinite ease-in-out;
}
@-webkit-keyframes moveAndGlow {
  25% {
    background: #ccc;
    -webkit-transform: scale(.5);
    transform: scale(.5);
    margin-top: 25px;
  }
  50% {
    left: 100%;
    margin-left: -100px;
    background: #888;
    -webkit-transform: scale(1);
    transform: scale(1);
    margin-top: 0;
  }
  75% {
    background: #000;
    -webkit-transform: scale(1.5);
    transform: scale(1.5);
    margin-top: 25px;
  }
}
@keyframes moveAndGlow {
  25% {
    background: #ccc;
    -webkit-transform: scale(.5);
    transform: scale(.5);
    margin-top: 25px;
  }
  50% {
    left: 100%;
    margin-left: -100px;
    background: #888;
    -webkit-transform: scale(1);
    transform: scale(1);
    margin-top: 0;
  }
  75% {
    background: #000;
    -webkit-transform: scale(1.5);
    transform: scale(1.5);
    margin-top: 25px;
  }
}
<div class="overlay">
  <span class="circle move"></span>
</div>

最佳答案

如果你想在 3d 环境中移动你的元素,你可以使用 perspective属性和实际的 3d 旋转。

现在您正在位置之间的直线上制作动画,因此几乎不可能模拟旋转。我构建了以下示例,您需要调整大小以适应您的元素,但您应该明白了。

另请注意,我将渐变背景放在一个伪元素中,以便它出现在移动对象的前面:

.overlay {
  height: 200px;
  position: relative;
  width: 40%;
  margin: auto;
  perspective:500px;
  margin-top:50px;
}
.overlay:after{
  content:'';
  position:absolute;
  top:-100px; left:-10%;
  width:120%; height:100%;
  background-image: repeating-linear-gradient(90deg, transparent, transparent 1%, rgb(255, 255, 255) 2%, rgb(255, 255, 255) 2%);
}

.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #888;
  position: absolute;
  z-index: -1;
  left: 50%;
  margin-left:-50px;
  transform: rotateY(0deg) translateX(-100px) rotateY(0deg);
  display: inline-block;
}

.move {
  animation: moveAndGlow 2s infinite linear;
}

@keyframes moveAndGlow {
  to{  transform:rotateY(360deg) translateX(-100px) rotateY(-360deg); }
}
<div class="overlay">
  <span class="circle move"></span>
</div>

关于平滑 3D 革命的 CSS3 动画功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483361/

相关文章:

html - 我在标题下添加了一条动画线。但不能很好地传播

javascript - 只需添加一个类即可实现 SVG 动画

黄金比例的 CSS 设计?

javascript - .draggable 不适用于 div

javascript - SVG 线条全屏尺寸

android - 如何从旋转矢量 (Sensor.TYPE_ROTATION_VECTOR) 获取欧拉角

C++ DirectX9 不正确的 Sprite 旋转

javascript - 间隔旋转

javascript - 这个 CSS 动画链应该无限运行,但它没有

html - 制作子内容的 div 大小 - 包括边距