Aurelia - Animations

Dans ce chapitre, vous apprendrez à utiliser les animations CSS dans le framework Aurelia.

Étape 1 - Afficher

Notre vue aura un élément qui sera animé et un bouton pour déclencher le animateElement() fonction.

app.html

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

Étape 2 - Voir le modèle

Dans notre fichier JavaScript, nous importerons CssAnimatorplugin et l'injecter en tant que dépendance. leanimateElementLa fonction appellera l'animateur pour démarrer l'animation. L'animation sera créée à l'étape suivante.

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');
   }
}

Étape 3 - Style

Nous écrirons CSS à l'intérieur styles/styles.css fichier. .myAnimation-add est le point de départ d'une animation tandis que .myAnimation-remove est appelée lorsque l'animation est terminée.

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; }
}

Une fois la ANIMATEest cliqué sur le bouton, la couleur d'arrière-plan passe du bleu clair à une nuance foncée. Lorsque cette animation est terminée après trois secondes, l'élément disparaîtra à son état de départ.