Angular 2 - 动画过渡不工作
Angular 2 - Animation transition not working
我需要两种颜色之间的过渡 OnClick
。现在,这是我的代码:
打字稿
animations: [
trigger('menubarState', [
state('false', style({backgroundColor:'#43a047'})),
state('true', style({backgroundColor:'#333'})),
transition('false => true', animate('1s')),
transition('true => false', animate('1s'))
])
]
...
export class MenubarComponent {
menuActive: boolean = false;
onMenuClick () {
if (this.menuActive == false) {
this.menuActive = true;
} else {
this.menuActive = false;
}
}
}
HTML
<li [@menubarState]="menuActive" (click)="onMenuClick()">
<a><span class="material-icons icon">apps</span></a>
</li>
这确实改变了 background-color
。然而,这种变化是即时的,而不是过渡。
我正在使用 Chrome 最新版本。
你不能用CSS吗?
EX:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #FF0;
height: 200px;
width: 100px;
animation: fontbulger 2s infinite;
}
@keyframes fontbulger {
0% {
background-color: blue;
}
50% {
background-color: red;
}
100% {
background-color: blue;
}
}
</style>
<title>test</title>
</head>
<body>
<div></div>
</body>
</html>
我使用了动画,而 angular 2.It 中的 routing/clicking 也适用于 onClick。
将此代码用于所有类型的路由,您可以为单独的路由更改或点击使用单独的动画。
import { animate, AnimationEntryMetadata, state, style, transition, trigger } from '@angular/core';
export const slideInDownAnimation: AnimationEntryMetadata =
trigger('routeAnimation', [
state('*',
style({
opacity: 1,
backgroundColor:'#43a047',
transform: 'translateX(0)'
})
),
transition(':enter', [
style({
opacity: 0,
backgroundColor:'#333',
transform: 'translateX(-100%)'
}),
animate('0.2s ease-in')
]),
transition(':leave', [
animate('0.5s ease-out', style({
opacity: 0,
backgroundColor:'red',
transform: 'translateY(100%)'
}))
])
]);
你也可以根据自己的选择修改上面的代码
这困扰我的时间最长,解决它的方法是将我的状态变量从布尔类型更改为字符串类型。所以 - 不要跟踪 true
和 false
,而是跟踪 'true'
和 'false'
。根据 Angular 的文档:
An animation state is a string value that you define in your application code.
将您的 MenubarComponent class 更改为:
export class MenubarComponent {
menuActive: string = 'false';
onMenuClick () {
if (this.menuActive === 'false') {
this.menuActive = 'true';
} else {
this.menuActive = 'false';
}
}
}
我认为这很愚蠢。布尔值显然是二进制状态的不错选择。
更多信息:https://angular.io/guide/animations#states-and-transitions
扩展 Aaron Krauss 提供的答案。它在直接解释真/假值方面存在问题;但是,如果您不想引用字符串上下文,则可以将上面的 true 和 false 替换为数字 1 (true) 和 0 (false)。这证明可以解决我的问题,并且不需要任何烦人的字符串解释。
trigger('menubarState', [
state('0', style({backgroundColor:'#43a047'})),
state('1', style({backgroundColor:'#333'})),
transition('0 => 1', animate('1s')),
transition('1 => 0', animate('1s'))
])
进一步扩展 Aaron Krauss 的回答:
我不想将我的布尔值转换成字符串,所以我定义了一个 getter:
public menuActive: boolean = false;
get menuState() {
return this.menuActive ? 'active' : 'inactive'
}
然后在你的动画定义中(我合并了过渡,因为它们是相同的):
animations: [
trigger('menubarState', [
state('inactive', style({backgroundColor:'#43a047'})),
state('active', style({backgroundColor:'#333'})),
transition('inactive <=> active', animate('1s'))
])
]
为了完整起见,模板:
<li [@menubarState]="menuState" (click)="onMenuClick()">
<a><span class="material-icons icon">apps</span></a>
</li>
我不知道我的回答是否解决了这个问题,但我会说如果你使用 Ionic/Angular trigger/transition 不起作用,但只有 trigger/state.
如果您使用 Ionic/angular,您应该使用 Ionic Animations。
https://ionicframework.com/docs/utilities/animations
我需要两种颜色之间的过渡 OnClick
。现在,这是我的代码:
打字稿
animations: [
trigger('menubarState', [
state('false', style({backgroundColor:'#43a047'})),
state('true', style({backgroundColor:'#333'})),
transition('false => true', animate('1s')),
transition('true => false', animate('1s'))
])
]
...
export class MenubarComponent {
menuActive: boolean = false;
onMenuClick () {
if (this.menuActive == false) {
this.menuActive = true;
} else {
this.menuActive = false;
}
}
}
HTML
<li [@menubarState]="menuActive" (click)="onMenuClick()">
<a><span class="material-icons icon">apps</span></a>
</li>
这确实改变了 background-color
。然而,这种变化是即时的,而不是过渡。
我正在使用 Chrome 最新版本。
你不能用CSS吗?
EX:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #FF0;
height: 200px;
width: 100px;
animation: fontbulger 2s infinite;
}
@keyframes fontbulger {
0% {
background-color: blue;
}
50% {
background-color: red;
}
100% {
background-color: blue;
}
}
</style>
<title>test</title>
</head>
<body>
<div></div>
</body>
</html>
我使用了动画,而 angular 2.It 中的 routing/clicking 也适用于 onClick。 将此代码用于所有类型的路由,您可以为单独的路由更改或点击使用单独的动画。
import { animate, AnimationEntryMetadata, state, style, transition, trigger } from '@angular/core';
export const slideInDownAnimation: AnimationEntryMetadata =
trigger('routeAnimation', [
state('*',
style({
opacity: 1,
backgroundColor:'#43a047',
transform: 'translateX(0)'
})
),
transition(':enter', [
style({
opacity: 0,
backgroundColor:'#333',
transform: 'translateX(-100%)'
}),
animate('0.2s ease-in')
]),
transition(':leave', [
animate('0.5s ease-out', style({
opacity: 0,
backgroundColor:'red',
transform: 'translateY(100%)'
}))
])
]);
你也可以根据自己的选择修改上面的代码
这困扰我的时间最长,解决它的方法是将我的状态变量从布尔类型更改为字符串类型。所以 - 不要跟踪 true
和 false
,而是跟踪 'true'
和 'false'
。根据 Angular 的文档:
An animation state is a string value that you define in your application code.
将您的 MenubarComponent class 更改为:
export class MenubarComponent {
menuActive: string = 'false';
onMenuClick () {
if (this.menuActive === 'false') {
this.menuActive = 'true';
} else {
this.menuActive = 'false';
}
}
}
我认为这很愚蠢。布尔值显然是二进制状态的不错选择。
更多信息:https://angular.io/guide/animations#states-and-transitions
扩展 Aaron Krauss 提供的答案。它在直接解释真/假值方面存在问题;但是,如果您不想引用字符串上下文,则可以将上面的 true 和 false 替换为数字 1 (true) 和 0 (false)。这证明可以解决我的问题,并且不需要任何烦人的字符串解释。
trigger('menubarState', [
state('0', style({backgroundColor:'#43a047'})),
state('1', style({backgroundColor:'#333'})),
transition('0 => 1', animate('1s')),
transition('1 => 0', animate('1s'))
])
进一步扩展 Aaron Krauss 的回答: 我不想将我的布尔值转换成字符串,所以我定义了一个 getter:
public menuActive: boolean = false;
get menuState() {
return this.menuActive ? 'active' : 'inactive'
}
然后在你的动画定义中(我合并了过渡,因为它们是相同的):
animations: [
trigger('menubarState', [
state('inactive', style({backgroundColor:'#43a047'})),
state('active', style({backgroundColor:'#333'})),
transition('inactive <=> active', animate('1s'))
])
]
为了完整起见,模板:
<li [@menubarState]="menuState" (click)="onMenuClick()">
<a><span class="material-icons icon">apps</span></a>
</li>
我不知道我的回答是否解决了这个问题,但我会说如果你使用 Ionic/Angular trigger/transition 不起作用,但只有 trigger/state.
如果您使用 Ionic/angular,您应该使用 Ionic Animations。 https://ionicframework.com/docs/utilities/animations