angular 过度动画
1.在所需过度ts中引入
import {trigger, style, animate, transition, state} from '@angular/animations'
在app.module.ts 引入
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
2.设定过度方案
trigger(‘动画组名’[ state(‘动画名’,style({写CSS}), state(‘动画名2’,style({写CSS}) transition(‘动画名=>动画名2’,animate(‘运动时间’)) ])
3.所需元素绑定触发
[@动画组名]="判断条件?‘动画名’:‘动画名2’"
或者@动画组名
案例
html
<div style="height: 30px;width: 100px;border: 1px solid cornflowerblue" (mouseenter)="mousein()" (mouseleave)="mouseout()"></div> <div style="position: absolute" *ngIf="isshow" @myInsertRemoveTrigger > <ng-content class="mydiv"></ng-content> </div>
ts代码
import {Component, OnInit} from '@angular/core'; import {trigger, style, animate, transition, state} from '@angular/animations' @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'], animations: [ trigger('myInsertRemoveTrigger', [ transition(':enter', [ style({opacity: 0}), animate('100ms', style({opacity: 1})), ]), transition(':leave', [ animate('100ms', style({opacity: 0})) ]) ]), ], }) export class TestComponent implements OnInit { constructor() { } ngOnInit(): void { } isshow = false mousein() { this.isshow = true } mouseout() { this.isshow = false } }
别忘了在app.module.ts中引入
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!