【问题标题】:Element wobbles CSS元素摆动 CSS
【发布时间】:2019-05-16 17:21:42
【问题描述】:

我用 CSS 制作了一颗心,并让它像往常一样调整大小,每秒钟变小 - 变大。但我注意到了这种奇怪的摆动,我并没有真正放在那里,看着真的很痛苦。我知道很少有可能重复的问题,但它们并没有真正帮助。这是fiddle

html,
body,
.container {
  height: 100%;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.heart {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: red;
  transform: rotate(-45deg);
  transform-style: preserve-3d;
  animation: growMain 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

.heart:after,
.heart:before {
  background-color: red;
  content: "";
  border-radius: 50%;
  position: absolute;
  transform: translateZ(-1px);
}

.heart:after {
  animation: growAfter 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

.heart:before {
  animation: growBefore 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes growMain {
  from {
    width: 50px;
    height: 50px;
  }
  to {
    width: 80px;
    height: 80px;
  }
}

@keyframes growAfter {
  from {
    left: 25px;
    width: 50px;
    height: 50px;
  }
  to {
    left: 40px;
    width: 80px;
    height: 80px;
  }
}

@keyframes growBefore {
  from {
    top: -25px;
    width: 50px;
    height: 50px;
  }
  to {
    top: -40px;
    width: 80px;
    height: 80px;
  }
}

.inner {
  display: initial;
  transform: rotate(45deg);
}

.text {
  text-align: center;
  color: white;
  font-family: "Comic Sans MS";
  font-size: 100%;
  margin: 0;
}
<div class="container">
  <div class="heart">
    <div class="inner">
      <p class="text">Some text</p>
    </div>
  </div>
</div>

【问题讨论】:

  • 什么问题?
  • 它像疯了一样摇晃@DogukanCavus,至少在 Opera 和 Chrome 中,即使我没有在我的代码中编写它。也许在其他浏览器中不会发生
  • "...即使我没有在我的代码中写下它" - 我能给出的最好的建议是你应该总是在你的问题中描述你的问题,否则我们应该如何提供帮助?
  • @DavidThomas 我确实做到了,但我怎么知道问题是否也出现在您的浏览器上。正如我所说,心脏在颤抖,一点点但你可以看到它,看着很痛苦

标签: html css css-animations


【解决方案1】:

动画实际宽度/高度/位置往往表现不佳,尤其是像您在这里一样一次制作多个动画时。使用transform 移动/调整元素的效果往往更好。

在这种情况下,我建议设置心脏的初始大小,然后使用scale 转换来制作脉冲效果。使用这种方法,您还可以获得从三个动画到一个动画的额外好处,这对浏览器来说更容易处理,而且您不必担心将它们全部同步。

为了使文字不随心形收缩,可以在其周围放一个包装,并将文字绝对定位在包装的中心,在心的上方。然后只是改变心本身,而不是包装。 (或者,如果您希望文本与心脏一起缩小,请保持与现在相同的 HTML 结构。)

html,
body,
.container {
  height: 100%;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.heart-wrapper {
  position: relative;
  width: 80px;
  height: 80px;
}

.heart {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: red;
  transform: rotate(-45deg) scale(1);
  transform-style: preserve-3d;
  animation: pulse 1s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

.heart::before {
  left: 40px;
}
.heart::after {
  top: -40px;
}
.heart::after,
.heart::before {
  background-color: red;
  content: "";
  width: 80px;
  height: 80px;
  border-radius: 50%;
  position: absolute;
  transform: translateZ(-1px);
}

@keyframes pulse {
  from {
    transform: rotate(-45deg) scale(1);
  }
  to {
    transform: rotate(-45deg) scale(0.6);
  }
}

.text {
  position: absolute;
  top: 40%; /* slightly off-center to the top, so it appears centered when the heart shrinks */
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: white;
  font-family: "Comic Sans MS";
  font-size: 100%;
  margin: 0;
}
<div class="container">
  <div class="heart-wrapper">
    <div class="heart"></div>
    <div class="text">some text</div>
  </div>
</div>

【讨论】:

  • 谢谢@cjl750,解释为什么会出现延迟以及如何解决它,这就是我一直在寻找的!
猜你喜欢
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2012-11-27
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多