html - 有没有可能让这个div顺利移动?

标签 html css

我试图让这个 div 在改变颜色时平滑移动,但问题是就在它应该过渡到 #bad455 颜色之前,它短暂地停止了.

所以我想知道有什么方法可以让它顺利进行而不停止吗?

div {
  background-color: black;
  width: 300px;
  height: 300px;
  margin: 0 auto;
}

div:hover {
  animation-name: anim1;
  animation-duration: 3s;
}

@keyframes anim1 {
  0% {
    background-color: pink;
    margin-top: 10px;
  }
  50% {
    background-color: yellow;
    margin-top: 20px;
  }
  100% {
    background-color: #bad455;
    margin-top: 30px;
  }
}
<div></div>

最佳答案

您将迭代计数设置为无限,以便您的动画继续播放并将最后一个关键帧的边距设置回 0,以便它返回到默认状态。

div {
  background-color: black;
  width: 300px;
  height: 300px;
  margin: 0 auto;
}

div:hover {
  animation-name: anim1;
  animation-iteration-count: infinite;
  animation-duration: 3s;
}

@keyframes anim1 {
  0% {
    background-color: pink;
    margin-top: 10px;
  }
  50% {
    background-color: yellow;
    margin-top: 30px;
  }
  100% {
    background-color: #bad455;
    margin-top: 0px;
  }
}
<div></div>

关于html - 有没有可能让这个div顺利移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114713/

相关文章:

python - Django:没有 CSS 和正确文本的 html

重要的javascript div背景颜色

jquery - 悬停时 CSS 更改内容

PHP HTML Javascript 设计问题

jQuery Draggable() 和键盘控制

html - 更改包裹在 <a> 中的悬停元素符号颜色

CSS - 各种颜色的圆形边框

c# - 遮蔽元素名称改变ID

javascript - 使用 jquery 在多个表单上提交表单之前询问确认

javascript - 如何在javascript中传递链接名称?