目录

Aurelia - 动画( Animations)

在本章中,您将学习如何在Aurelia框架中使用CSS动画。

第1步 - 查看

我们的视图将有一个动画元素和一个触发animateElement()函数的按钮。

app.html

<template>
   <div class = "myElement"></div>
   <button click.delegate = "animateElement()">ANIMATE</button>
</template>

第2步 - 视图模型

在我们的JavaScript文件中,我们将导入CssAnimator插件并将其作为依赖项注入。 animateElement函数将调用动画师来启动动画。 动画将在下一步中创建。

import {CssAnimator} from 'aurelia-animator-css';
import {inject} from 'aurelia-framework';
@inject(CssAnimator, Element)
export class App {
   constructor(animator, element) {
      this.animator = animator;
      this.element = element;
   }
   animateElement() {
      var myElement = this.element.querySelector('.myElement');
      this.animator.animate(myElement, 'myAnimation');
   }
}

第3步 - 风格

我们将在styles/styles.css文件中编写CSS。 .myAnimation-add是动画的起点,而动画完成时会调用.myAnimation-remove

styles.css

.myElement {
   width:100px;
   height: 100px;
   border:1px solid blue;
}
.myAnimation-add {
   -webkit-animation: changeBack 3s;
   animation: changeBack 3s;
}
.myAnimation-remove {
   -webkit-animation: fadeIn 3s;
   animation: fadeIn 3s;
}
@-webkit-keyframes changeBack {
   0% { background-color: #e6efff; }
   25% { background-color: #4d91ff; }
   50% { background-color: #0058e6; }
   75% { background-color: #003180; }
   100% { background-color: #000a1a; }
}
@keyframes changeBack {
   0% { background-color: #000a1a; }
   25% { background-color: #003180; }
   50% { background-color: #0058e6; }
   75% { background-color: #4d91ff; }
   100% { background-color: #e6efff; }
}

单击ANIMATE按钮后,背景颜色将从浅蓝色变为深色。 当此动画在三秒后完成时,元素将淡入其开始状态。

Aurelia动画示例
↑回到顶部↑
WIKI教程 @2018