javascript - Firefox Quantum 上的动画问题

标签 javascript css firefox firefox-quantum

我注意到新的 Firefox Quantum 上的动画有问题。

当您第一次加载包含一些动画元素 display: none; 的页面时,当脚本将其切换为 .display = "block"; 时,您将错过整个动画,或者它的某些部分在开头,如果它长于几秒。

在下面的代码片段中查看:

var anims = document.getElementsByClassName("anim"),
    time = document.getElementById("time"),
    interval = null;

function animate() {
  for (var i = 0; i < anims.length; i++)
    anims[i].style.display = "block";
}

function timer(sec) {
  time.textContent = sec--;
  interval = setInterval(function () {
    time.textContent = sec >= 0 ? sec-- : clearInterval(interval) || "";
  }, 1000);
}

// Directly after click
button0.addEventListener("click", animate);

// One seconds after click
button1.addEventListener("click", function () {
  timer(1);
  setTimeout(animate, 1000);
});

// Two seconds after click
button2.addEventListener("click", function () {
  timer(2);
  setTimeout(animate, 2000);
});

// Three seconds after click
button3.addEventListener("click", function () {
  timer(3);
  setTimeout(animate, 3000);
});

// Hide the divs
reset.addEventListener("click", function () {
  for (var i = 0; i < anims.length; i++)
    anims[i].style.display = "none";
});
body {
  font-family: arial;
}

body > div {
  margin-bottom: 10px;
}

#result {
  background-color: #e5f3ff;
  height: 120px;
  padding: 10px;
}

.anim {
  display: none;
  float: left;
  margin: 10px;
  width: 50px;
  height: 50px;
  border-radius: 5px;
  animation: animate 1.5s;
}

#anim1 {
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  
  /* Only one iteration iteration (default) */
  /* This one will not be animated */
}

#anim2 {
  background-color: #fddb92;
  animation-iteration-count: 3; 
  
  /* Three iterations */
  /* Only one iteration will be seen */
}

#anim3 {
  background-image: linear-gradient(45deg, #ff9a9e 0%, #fad0c4 99%, #fad0c4 100%);
  animation-iteration-count: infinite; 
  
  /* Infinite */
  /* No visible problem */
}

@keyframes animate {
  50% {
    transform: translate(80%, 100%) rotate(-360deg);
  }
}
<div>
  <span><strong>Reload the snippet</strong>
  before clicking another button for viewing the issue
  <br/><strong>Or,</strong>
  <em>Reset</em> (display: "none") before clicking a button to view with no issue: </span>
</div>

<div>
  <button id="button0">On click</button>
  <button id="button1">1 sec timeout</button>
  <button id="button2">2 sec timeout</button>
  <button id="button3">3 sec timeout</button>
  <button id="reset">Reset</button>
  <span id="time"></span>
</div>

<div id="result">
  <div id="anim1" class="anim"></div>
  <div id="anim2" class="anim"></div>
  <div id="anim3" class="anim"></div>
</div>

你会注意到无限动画显然没有任何问题,但其他两个显然有问题。

那怎么解决呢?

注意:

  • 您必须使用 Firefox Quantum 才能查看。
  • 我在 Google Chrome 上尝试了相同的代码段,一切正常。

最佳答案

经过测试,很确定它可以通过使用类解决所有浏览器的问题。有更多的方法来处理它,但将动画放在一个新类中,只有在单击按钮后才添加该类。

在 CSS 中我将动画属性移到了一个新类中,新类还添加了 block 样式。

.anim-start {
  display: block;
  animation: animate 1.5s;
}

在 JS 中,我只将 style.display='block' 更改为 anims[i].classList.add('anim-start');

参见: https://jsfiddle.net/0mgqd2ko/1/

使用新类的这个方法就更简单了。例如,如果你想从不透明度 0 过渡到 1 怎么办?从不显示开始时很难做到这一点。如果您只想使用可见性以便元素仍然占用空间怎么办?

关于javascript - Firefox Quantum 上的动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47739338/

相关文章:

javascript - 下拉菜单(选择)——选择一个项目,并根据选择显示图片

javascript - 如何将 jquery ajax if else 语句转换为 switch 语句

电子邮件收件箱中未显示 HTML 背景

jQuery:边距动画适用于 Chrome,不适用于 Firefox 和 IE

javascript - jQuery getScript 问题

javascript - 如何动态设置 Angular Material md-datepicker 指令的 md-min-date/md-max-date?

css - 连接 CSS 与缓存控制

html - 如何在 Bootstrap 按钮内制作响应式文本

火狐插件 : extension icon not showing

javascript - 无法在 'removeChild' : parameter 1 is not of type 'Node' 上执行 'Node'