Web Animation Api 入门

作者: 码农氵 | 来源:发表于2017-05-20 20:00 被阅读870次
Getting Started With The JavaScript Web Animation API

在网页中使用动画提供了更好的用户体验,例如抽屉式菜单。

目前为止,web动画可以通过css3 transitions,css3 keyframes或者其他的动画库(animate.css、Velocity、tween),现在我们可以使用js编写更加自由的web动画,那就是web animation。

创建动画

我们分别用css3和web animation api写个简单的demo用来展示web animation的特性。

Demo

  • css方法

var square = document.getElementById('square');
    
square.addEventListener('click', function() {
    square.className += " animate";
});

.animate {
    animation-name: move-and-change-color;   
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
}

@keyframes move-and-change-color {
    0% {
        transform: translateX(0);
    }

    80% {
        transform: translateX(100px);
        background-color: #2196F3;
    }

    100% {
        transform: translateX(100px);
        background-color: #EF5350;
    }
}

  • Web Animation方法
var moveAndChangeColor = [
    { 
        transform: 'translateX(0)',
        background: '#2196F3'    // blue
    },
    { 
        offset: 0.8,
        transform: 'translateX(100px)', 
        background: '#2196F3'    // blue
    },
    {
        transform: 'translateX(100px)',
        background: '#EF5350'    // red
    }
]; //数组中的每个对象代表一个动画状态

var circle = document.getElementById('circle');
  
circle.addEventListener('click', function() {
    circle.animate(moveAndChangeColor, {
        duration: 400,
        fill: 'forwards'
    });
});

控制动画

通过上面的例子可以简单的对比出,css3方法局限性较大,并不适合复杂的动画,也不易于控制,而Web Animation Api适合复杂动画,并且易于控制。

var animation = elem.animate(transitions, options);

Web Animation Api 提供如下方法:

  • pause() – 暂停动画
  • play() – 播放动画
  • reverse() – 反向播放
  • finish() – 立即结束动画
  • cancel() – 取消动画并回到初始状态

具体使用方法请看Demo

属性和事件监听

动画运行的过程中会通过animate返回动画属性对象,比如动画播放速率-playbackrate,移步Demo

此外,我们还可以监听finishcancel事件做点其他有意义的事情

spinnerAnimation.addEventListener('finish', function() {
    // Animation has completed or .finish() has been called.
    doSomething();
});

spinnerAnimation.addEventListener('cancel', function() {
    // Animation has been canceled.    
    doSomething();
});

兼容性

大部分chrome、firefox都支持Web Animation Api,其他的例如ios、安卓都不支持,详情请查看Caniuse

对于不支持的可以是用polyfill

相关资料

原文地址

Getting Started With The JavaScript Web Animation API

相关文章

  • Web Animation Api 入门

    在网页中使用动画提供了更好的用户体验,例如抽屉式菜单。 目前为止,web动画可以通过css3 transition...

  • animation参考

    web前端入门到实战:10分钟入门 CSS3 Animation - 前端天宇的文章 - 知乎https://zh...

  • web api入门

    HTTP不只是服务了网页。这也是一个强大的平台,构建公开服务和数据API。 HTTP是简单,灵活,并无处不在的,几...

  • ASP.NET Web API 2 (C#)入门

    微软官方入门文档 https://docs.microsoft.com/zh-cn/aspnet/web-api/...

  • 数据可视化:matplotlib animation 绘制动画(

    ref: https://matplotlib.org/3.1.1/api/animation_api.html[...

  • Android中的动画

    一、View Animation 视图动画(API Level 1引入) (一)、Tween Animation ...

  • JS-API、DOM-day07-5.6

    Web API Web API介绍 API的概念 API(Application Programming Inte...

  • Web API(备忘)

    Web API Web API介绍 API的概念 API(Application Programming Inte...

  • Android View Animation

    Animation -- View Animation 官方Api 帧动画 帧动画就是通常所说的Frame动画。F...

  • [R]高德爬虫

    主要是基于R语言和高德提供的web API进行编写 以下是高德开放平台提供的入门指南:第 1 步:申请”Web服务...

网友评论

    本文标题:Web Animation Api 入门

    本文链接:https://www.haomeiwen.com/subject/labpxxtx.html