我一直在使用 Web Animations API,并且发现它非常直观,可以开始使用。
从我的角度来看,我想制作一个非常简单的动画,其中一个元素沿一个方向旋转,并且在按下按钮后沿另一个方向旋转。使用标准反向方法时,动画会因为时间用完而停止。
我尝试使用 setKeyframes 方法来更改变换方向,这似乎在 Chrome 中不起作用。在我的 Firefox 开发者版中,它工作得很好......
有人可以查看我的方法并告诉我是否遗漏了什么吗?请记住,总的来说,我对 JS 很陌生。
提前致谢,
尼克拉斯
//This is what my JS Code looks like
//I have commented out the parts that throw a error inside Chrome
var fan = document.getElementById('fan');
var cw = new KeyframeEffect(
fan, [{
transform: "rotate(0)"
},
{
transform: "rotate(360deg)",
}
], {
// timing options
duration: 8000,
iterations: Infinity,
});
//creates new Animation object and starts it in clockwise rotation
var fananim = new Animation(cw, document.timeline);
//Stops Animation so it doesnt run automatically
fananim.pause();
var clockwise = function() {
/* cw.setKeyframes(
[{
transform: "rotate(0)"
},
{
transform: "rotate(360deg)",
}
]
); */
fananim.play();
}
var counterclockwise = function() {
/* cw.setKeyframes(
[{
transform: "rotate(0)"
},
{
transform: "rotate(-360deg)",
}
]
); */
fananim.play();
//this leads to the time running out and the animation stopping
//fananim.reverse();
}
var stop = function() {
fananim.pause();
}
// Click Handlers for Buttons on Control Baord
var ela = document.getElementById("cw");
ela.addEventListener("click", clockwise, false);
var elb = document.getElementById("ccw");
elb.addEventListener("click", counterclockwise, false);
var elc = document.getElementById("stop");
elc.addEventListener("click", stop, false);