ionic3 添加动画

  • 为了让ionic app 的效果更接近原生,动画效果必不可少,在此列举两种方式添加动画。

第一种方法

  • 1.install web-animations-js
npm install web-animations-js
  • 2.引用
//在main.ts 导入
import 'web-animations-js/web-animations.min';
//在app.module.ts
 import { BrowserAnimationsModule } from '@angular/platform-browser/animations'

  imports: [
    BrowserModule,
    BrowserAnimationsModule,   
    IonicModule.forRoot(MyApp)
  ]
  • 3.用法
// in HTML 
<button ion-button (click)="fadeIt()" >fade it!</button>
<p [@fading] = 'visableState'>hhhahahahahhaha</p>
// in .ts
import { trigger ,state,transition,animate,style} from "@angular/animations";
@Component({
  selector: 'page-home',
  templateUrl: 'home.html',

  animations:[
      trigger('fading',[
         state('visable',style({
            opacity: 1
         })),
         state('invisable', style({
           opacity: 0
         })),
         transition('* => *',animate('.5s'))
      ])
  ]
})
// in export class  xxPage
 visableState = 'visable';
fadeIt(){
      this.visableState = (this.visableState == 'visable')?'invisable':'visable';
   }

第二种方法

    1. install css-animator
npm install css-animator
// in index.html
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
  • 2.引用
import { AnimatesDirective, AnimationService } from 'css-animator'

 declarations: [
    MyApp,
    AnimatesDirective
  ],
providers: [
    StatusBar,
    SplashScreen,
    AnimationService,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
  • 3.用法
// in HTML  点击即可看到动画
<div animates #animation="animates" text-center>
     <button ion-button (click)="animation.start({type:'shake',duration:'1000'})" >click it!</button>
 </div>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容