css - 动画运行时禁用过渡

标签 css css-transitions css-animations

问题演示:https://jsfiddle.net/t0qsek8n/1/

<div class="test" id="test">Test text</div>
.test {
  position: relative;
  top: 0px;
  border: 1px solid #ccc;
  animation: test 5s;
  transition: top 1s;
}

@keyframes test {
  0% {
    opacity: 0;
    transition: none;
  }
  100% {
    opacity: 1;
    transition: none;
  }
}
const test = document.getElementById('test');
setTimeout(() => {
    test.style.top = "100px"
}, 1000);

我预计如果 top 属性的值被 JS 更改,转换 transition: top 1000ms 不会发生,因为 transition: none 提供了@keyframes test,但实际上,转换发生了。 我无法理解为什么 keyframes 中的 transition 值不会覆盖 transition 的任何现有定义。

最佳答案

让我们再举一个使用 display 的例子:

.test {
  position: relative;
  top: 0px;
  border: 1px solid #ccc;
  animation: test 5s forwards;
}

@keyframes test {
  0% {
    opacity: 0;
    display: none;
  }
  100% {
    opacity: 1;
    display: none;
  }
}
<div class="test" id="test">
  Test text
</div>

从逻辑上讲,我们希望永远不会看到该元素,因为我们设置了 display:none 并且我们将动画设置为forwards,但显示被简单地忽略了,因为它不能动画化。与 transition 的逻辑相同,因为它是我们无法设置动画的属性 ref .

基本上,任何无法动画的属性在与关键帧一起使用时都会被忽略。

Properties that aren't specified in every keyframe are interpolated if possible — properties that can't be interpolated are dropped from the animation. ref

关于css - 动画运行时禁用过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809561/

相关文章:

css - 位置固定不适用于标题

css - 这两种修复折叠父级和 float 子级的方法是相同的,但是为什么呢?

html - 以自适应宽度和固定纵横比显示图像

css - .btn 悬停时改变背景颜色,无论我做什么

单个 div 的 CSS 动画延迟

css - 动画 svg 描边不完整

css - 移动设备上的平滑关键帧动画?

jquery - Chrome 浏览器中的 CSS3 转换

css - 如何仅在转换 : translateX()? 上应用转换

html - 定时动画