javascript - 更改在 IE 11 中不起作用的 DIV 的旋转速度,使用 JS 更新 CSS 动画属性

标签 javascript html css internet-explorer

我正在尝试使用简单的按钮更改 DIV 的旋转速度。它适用于 Chrome,但不适用于 IE。这是IE的限制吗?

var speed = 3;
document.getElementById("speedText").innerHTML = speed + "s";

function changeSpeed(change) {
  speed = speed + change;
  document.getElementById("speedText").innerHTML = speed + "s";
  document.getElementById("rotationDiv").style.animationDuration = speed + "s";
  $("#rotationDiv").load(location.href + " #rotationDiv");
}
#rotationDiv {
  width: 200px;
  height: 200px;
  background-color: blue;
  animation-name: spin;
  animation-duration: 3s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rotationDiv"></div>
<p id="speedText"></p>
<button id="button1" onclick="changeSpeed(-1)">Speed Up</button>
<button id="button2" onclick="changeSpeed(1)">Slow Down</button>

最佳答案

您必须删除并重新添加动画才能让它接受更改。

更改速度后,将animation-name 更改为其他内容,然后在setTimeout 中,将animation-name 设置回去。这有点 hack,但它确实有效。

var speed = 3;
document.getElementById("speedText").innerHTML = speed + "s";

function changeSpeed(change) {
  speed = speed + change;
  document.getElementById("speedText").innerHTML = speed + "s";
  document.getElementById("rotationDiv").style.animationDuration = speed + "s";
  document.getElementById("rotationDiv").style.animationName = "x";
  setTimeout(function(){
    document.getElementById("rotationDiv").style.animationName = "";
  },0);
  $("#rotationDiv").load(location.href + " #rotationDiv");
}
#rotationDiv {
  width: 50px;
  height: 50px;
  margin-left: 10px;
  background-color: blue;
  animation-name: spin;
  animation-duration: 3s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button1" onclick="changeSpeed(-1)">Speed Up</button>
<button id="button2" onclick="changeSpeed(1)">Slow Down</button>
<p id="speedText"></p>
<div id="rotationDiv"></div>

关于javascript - 更改在 IE 11 中不起作用的 DIV 的旋转速度,使用 JS 更新 CSS 动画属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56816517/

相关文章:

javascript - 子 socket 中的 Angular 子路线

javascript - 如何运行验收测试?

html - 为什么这两个 ul li a 列表不同

javascript - 在滚动条上淡入/淡出背景

javascript - 为什么 mousemove 事件会导致 Chrome 中的数字输入失效?

css - 当父元素具有不同的不透明度时,如何使子元素的不透明度为 100%?

用于图表集成 WinJS 的 Javascript 库

javascript - Chrome 扩展消息传递 : response not sent

html - 如何使用 CSS 创建一个可缩放到父 div 高度的三 Angular 形?

javascript - 谷歌如何将一个选项卡中的更改(如文件重命名)反射(reflect)到驱动器中的其他选项卡?