初识WAAPI
element.animate(effect,options);
<div class="move"></div>
document.getElementById("move").animate(
[
{
offset:0,
opacity:0,
transform:'translate(0,0)'
},
{
offset:0.25,
transform:'translate(100px,0)'
},{
offset:0.5,
transform:'translate(200px,0)'
},{
offset:0.75,
transform:'translate(100px,0)'
},{
offset:1,
opacity:1,
transform:'translate(0,0)'
}
],{
delay: 0,
endDelay: 0,
fill: 'both',
iterationStart: 0,
iterations: 50,
duration: 1000,
direction: 'normal',
easing: 'linear'
});
var anima=document.body.animate()
anima.playState //返回动画状态 running,paused,finished
anima.pause() //暂停 pause
anima.play() //播放 running
anima.cancel() // idle
anima.finish() //结束 finished
anima.onfinish=function(e){}
anima.playbackRate
除了直接设置外,在WAAPI中还有另一种动画方式:
new Animation(effect,timeline)
另外,要启动动画,我们还需要WAAPI中的new keyFrameEffect(element,keyframesSet,keyframeOptions)和play()方法。
var b=document.getElementById("move");
var animeKeyframes={
transform:['translate(0,0)','translate(100px,0)','translate(200px,0)','translate(100px,0)','translate(0,0)'],
opacity:[0,1]
};
var animeOptions = {
delay: 0,
endDelay: 0,
fill: 'both',
iterationStart: 0,
iterations: 50,
duration: 1000,
direction: 'normal',
easing: 'cubic-bezier(.6, 0, 1, .6)'
};
var effect = new KeyframeEffect(b, animeKeyframes, animeOptions);
var animation = new Animation(effect,b.ownerDocument.timeline);
animation.play();
动画效果如下:
注意:由于目前浏览器对此方法的支持度不是很高,要想在浏览器看到有效果,需要引入一个polyfill。
<script src="https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.2.1/web-animations-next.min.js"></script>
参考自:大漠穷秋的《WAAPI入门》(http://www.w3cplus.com/animation/waapi-basic-intro.html)