html - CSS sidenav 文本不会在 ngIf 或 sidenav 打开时淡入
问题描述
我想在 sidenav 上显示文本以在打开时淡入并在关闭时淡出。我不喜欢打开 sidenav 时文本只是“弹出”的方式。
我这里有这个动画模板
export const animateText = trigger('animateText', [
state('hide',
style({
'display': 'none',
opacity: 0,
})
),
state('show',
style({
'display': 'block',
opacity: 1,
})
),
transition('close => open', animate('1500s ease-in')),
transition('open => close', animate('1500s ease-out')),
]);
这是我的html
<div class="sidenav_container" [@onSideNavChange]="sideNavState ? 'open' : 'close'">
<div fxLayout="column" fxLayoutGap="10px" style="height: 100%;">
<div class="user_menu text-center">
<mat-nav-list >
<a mat-list-item >
<img class="jim" src="https://a57.foxnews.com/media2.foxnews.com/BrightCove/694940094001/2018/06/21/931/524/694940094001_5800293009001_5800284148001-vs.jpg?ve=1&tl=1" alt="">
<span [@animateText]="linkText ? 'show' : 'hide'">{{ page?.name }} </span>
</a>
</mat-nav-list>
<mat-divider></mat-divider>
</div>
<div>
<header style="text-align:center; background-color:lightgray; color:royalblue;">Links</header>
<mat-nav-list>
<a mat-list-item *ngFor="let page of pages">
<mat-icon style="padding-right:5px;">{{page?.icon}}</mat-icon>
<span [@animateText]="linkText ? 'show' : 'hide'">{{ page?.name }} </span>
</a>
</mat-nav-list>
</div>
</div>
<span class="spacer"></span>
<div fxLayout="row" fxLayoutAlign="end end" style="padding: 0px 10px;">
<button mat-icon-button (click)="onSinenavToggle()">
<mat-icon *ngIf="sideNavState">arrow_left</mat-icon>
<mat-icon *ngIf="!sideNavState">arrow_right</mat-icon>
</button>
</div>
</div>
这是显示我的意思的堆栈闪电战。当您打开 sidenav 容器时,文本会弹出或出现。我想让它淡入
解决方案
看起来您在animateText
动画中的过渡中犯了一个小错误:
transition('close => open', animate('1500s ease-in')),
transition('open => close', animate('1500s ease-out')),
应该是:
transition('hide => show', animate('1500ms ease-in')),
transition('show => hide', animate('1500ms ease-out'))
此外,无法使用该display
属性创建动画。您应该使用max-width
or transfrom: scale()
。
一个例子是:
export const animateText = trigger('animateText', [
state('hide',
style({
opacity: 0,
'max-width': 0,
transform: 'scale(.5)'
})
),
state('show',
style({
opacity: 1,
'max-width': '200px',
transform: 'scale(1)'
})
),
transition('hide => show', animate('250ms ease-in')),
transition('show => hide', animate('250ms ease-out'))
]);
编辑:您还可以查看关键帧动画以获得更复杂的动画。使用关键帧可以使用该display: none
属性。
transition('show => hide', [
animate(
`500ms linear`,
keyframes([
style({ transform: 'scale(1)', opacity: 1, offset: 0 }),
style({ transform: 'scale(.5)', opacity: 0, offset: .99 }),
style({ transform: 'scale(.5)', opacity: 0, display: 'none', offset: 1 }),
])
)
])
(offset
表示动画进度)
上面的示例创建了一个淡出动画,并display: none
在动画达到 100% 后添加了一个。因为在display: none
最后添加了,所以它不会破坏动画。
推荐阅读
- python - 使用python多线程运行无限循环
- javascript - webpack热模块更换不起作用
- c# - 如何解决第 6 步?包含第 6 步的空值......为什么第 6 步包含空值(Nullexception)?
- javascript - 如何在if语句所在的for循环之外返回if语句的else
- vb.net - 类对象属性,它引用相同对象类型的另一个对象实例
- angularjs - 将现有应用程序从 Flex 迁移到 AngularJs
- javascript - 如何将数组从 Javascript 发送到 Laravel 服务器?
- qt - 如何使用 QT 将 udp 音频流录制到 wav 文件
- sql - 使用 54 个 SELECT 优化视图——更新为使用的流程
- java - 为什么这个 IntStream.mapToObj 映射器没有用 Stream.count 评估?