我正在尝试使用 Web Animations API ( https://developer.mozilla.org/en-US/docs/Web/API/Element/animate ) 合成一些动画。我想播放一个可以缩放我的项目的介绍动画,然后让它对 hoover 和 click 事件做出反应以播放一些额外的动画。我认为可以使用最近在 Firefox 和 Chrome 中引入的“复合”属性来组合动画(使用此作为参考:https ://css-tricks.com/additive-animation-web-animations-api/ ),但它似乎仍然取代了我之前声明的动画。
我把代码弄乱了:http: //jsfiddle.net/v0mk9eg4/51/
var item = document.getElementById("item");
var introFrames = [
{"transform": "translate(-50%, -50%) scale(0,0) translate(0, 0)"},
{"transform": "translate(-50%, -50%) scale(1,1) translate(0, 0)"}
]
var introOpts = {
"duration": 5000,
"iterations": 1,
"easing": "cubic-bezier(0.175, 0.885, 0.32, 1.275)"
}
var introAnim = item.animate(introFrames, introOpts);
introAnim.pause();
var hooverFrames = [
{"transform": "translate3d(0, 0, 0)"},
{"transform": "translate3d(-50%, -50%, 0)"}
]
var hooverOpts = {
"duration": 1000,
"iterations": 1,
"easing": "cubic-bezier(0.175, 0.885, 0.32, 1.275)",
"fill": "both",
"composite": 'add'
}
var hooverAnim = item.animate(hooverFrames, hooverOpts);
hooverAnim.pause();
var clickFrames = [
{"transform": "translate(0, 0)"},
{"transform": "translate(-50%, 0)"},
{"transform": "translate(0, 0)"}
]
var clickOpts = {
"duration": 500,
"iterations": 1,
"easing": "cubic-bezier(0.175, 0.885, 0.32, 1.275)",
"fill": "both",
"composite": 'add'
}
var clickAnim = item.animate(clickFrames, clickOpts);
clickAnim.pause();
introAnim.play();
function playHooverAnim(direction) {
console.log('HOOVER', direction);
hooverAnim.playbackRate = direction;
hooverAnim.play();
}
function playClickAnim() {
console.log('CLICK');
clickAnim.play();
}
注释掉 clickAnim 时播放 hooverAnim,注释掉 hooverAnim 时播放 introAnim。多个动画应该如何组合在一起播放?