【问题标题】:CSS animation interfering with transform transitionCSS动画干扰变换过渡
【发布时间】:2020-10-15 11:04:52
【问题描述】:

我想在 circle 类上应用悬停动画并单击 circle 我希望它平滑地过渡到所需位置,但我无法使过渡工作。没有悬停动画,过渡效果很好,但是一旦我在悬停时添加动画,过渡就不再适用了。

这是代码

<div class="doc">
<!--   TRANSITION NOT WORKING W/ HOVER ANIMATION -->
  <div id="mainContainer" class="container">
  <div id="circle" class="circle"></div>
  </div>
</div>

.container{
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100px;
  height: 100px;
  border: 3px solid black;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 25px;
  background: red;
  transition: all 1s ease;
}

.move {
  transform: translateX(50px);
}

.container:hover > .circle {
  animation: moveUp 1s infinite ease;
}

.container:hover > .move {
  animation: none;
}


@keyframes moveUp {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
  80% {
    transform: translateY(0);
  }
}
document.getElementById("mainContainer").onclick = function() {
var element = document.getElementById("circle");
  element.classList.add("move");
}

这是小提琴: https://jsfiddle.net/bshm0xca/58/

右边的框是想要的过渡效果 左边的框有一个悬停动画,但是一旦我点击容器,圆圈会捕捉到变换的位置,而不是过渡到它。

为什么会这样??如何让圆圈在悬停时进行动画处理并在点击时进行转换?

谢谢!

【问题讨论】:

  • 我认为如果您将.circle 包裹在父 div 周围,然后自行控制其状态会更容易

标签: javascript html css animation


【解决方案1】:

你只需要使用伪选择器:hover:active 没有js。

【讨论】:

  • 这种情况下的问题是,如果我在圆圈上有一个悬停动画,并且我希望它在点击时移动到 X=10,则不会应用过渡效果 - 圆圈只是捕捉到 X = 10。我希望它应用transition: all 1s ease; 这有意义吗?
  • 您似乎已经用上次编辑@okirim 覆盖了大部分答案。你能做一个包含你答案所有相关部分的新编辑吗?
【解决方案2】:

看看我在下面整理的示例,请注意我必须插入一个 div 来控制circle 转换状态。不混合animationtransition 的持续时间。

对于更复杂的动画序列,你应该看看transition events

document.getElementById("mainContainer").onclick = function() {
    const element = document.getElementById("circle");
    element.classList.toggle("move");
  
  element.addEventListener('transitionstart', () => {
    element.classList.add('moving')
  });
  
  element.addEventListener('transitionend', () => {
    element.classList.remove('moving');
  });
}
.doc {
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  width: 300px;
  height: 100%;
}

.circle {
  transition: all 2.5s ease;
}

.circle-bubble {
  width: 50px;
  height: 50px;
  border-radius: 25px;
  background: red;
}

.container{
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100px;
  height: 100px;
  border: 3px solid black;
}

.move {
  transform: translateX(50px);
}

.container:hover .circle:not(.moving) .circle-bubble {
  animation: moveUp .5s infinite ease;
}


@keyframes moveUp {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-15px);
  }
  80% {
    transform: translateY(0);
  }
}
<div class="doc">
  <div id="mainContainer" class="container">
    <div id="circle" class="circle">
      <div class="circle-bubble"></div>
    </div>
  </div>
</div>

【讨论】:

  • 谢谢!这行得通 - 我不明白为什么你一定需要一个专用于控制动画的容器(我试过没有它但动画不起作用)为什么有必要?
  • 例如,如果我拿走circle-bubble 类并执行以下操作: ``` .container:hover .circle:not(.movi​​ng).circle:not(.move) { 动画:moveUp 1s 无限轻松; } ``` 为什么它最终会捕捉而不是再次过渡?小提琴:jsfiddle.net/89k0grnb/15
  • 请看一下这个示例jsfiddle.net/j07hq34y,我尝试只使用一个 div,将持续时间增加到 3 秒,看看情况如何。这里的要点是同时使用animationtransition 可能会因为它的状态而搞乱你的动画意图。 transform 不会像 transformX(50px) transformY(-8px) 那样组合当前值,这就是为什么你会看到它在捕捉。在实际场景中尝试分解成小部分,以便您可以更轻松地控制
  • 你的 CSS 选择器 `.container:hover .circle:not(.movi​​ng).circle:not(.move)` 错了,应该是.container:hover .circle:not(.moving)
猜你喜欢
  • 2016-11-03
  • 2017-01-25
  • 2014-07-18
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2020-03-20
  • 1970-01-01
相关资源
最近更新 更多