Apply different animations for elements in *ngFor
up vote
1
down vote
favorite
so I'm trying to apply different animations for elements in an *ngFor loop. The simplest example would be the following:
@Component({
selector: 'app-anims',
templateUrl: './anims.component.html',
styleUrls: ['./anims.component.css'],
animations: [
trigger('scrolledToAnimation0', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "yellow"
})),
transition('first => second', [
animate('1s')
]),
]),
trigger('scrolledToAnimation1', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "green"
})),
transition('first => second', [
animate('0.40s')
]),
])
]
})
scrolledTo: boolean = false;
divElements: string = ["foo", "bar"];
@HostListener('window:scroll', ['$event'])
checkScroll() {
let componentPosition = this.el.nativeElement.offsetTop0;
let scrollPosition = window.pageYOffset;
if (scrollPosition >= componentPosition && this.scrolledTo === false) {
this.scrolledTo = true;
}
}
HTML:
<div *ngFor="div of divElements; let i = index" [@scrolledToAnimation0]="???" [@scrolledtoAnimation1]="???"></div>
I tried several approaches and none of them seem to work. Any help is appreciated - thanks in advance.
angular angular-animations
add a comment |
up vote
1
down vote
favorite
so I'm trying to apply different animations for elements in an *ngFor loop. The simplest example would be the following:
@Component({
selector: 'app-anims',
templateUrl: './anims.component.html',
styleUrls: ['./anims.component.css'],
animations: [
trigger('scrolledToAnimation0', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "yellow"
})),
transition('first => second', [
animate('1s')
]),
]),
trigger('scrolledToAnimation1', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "green"
})),
transition('first => second', [
animate('0.40s')
]),
])
]
})
scrolledTo: boolean = false;
divElements: string = ["foo", "bar"];
@HostListener('window:scroll', ['$event'])
checkScroll() {
let componentPosition = this.el.nativeElement.offsetTop0;
let scrollPosition = window.pageYOffset;
if (scrollPosition >= componentPosition && this.scrolledTo === false) {
this.scrolledTo = true;
}
}
HTML:
<div *ngFor="div of divElements; let i = index" [@scrolledToAnimation0]="???" [@scrolledtoAnimation1]="???"></div>
I tried several approaches and none of them seem to work. Any help is appreciated - thanks in advance.
angular angular-animations
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
so I'm trying to apply different animations for elements in an *ngFor loop. The simplest example would be the following:
@Component({
selector: 'app-anims',
templateUrl: './anims.component.html',
styleUrls: ['./anims.component.css'],
animations: [
trigger('scrolledToAnimation0', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "yellow"
})),
transition('first => second', [
animate('1s')
]),
]),
trigger('scrolledToAnimation1', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "green"
})),
transition('first => second', [
animate('0.40s')
]),
])
]
})
scrolledTo: boolean = false;
divElements: string = ["foo", "bar"];
@HostListener('window:scroll', ['$event'])
checkScroll() {
let componentPosition = this.el.nativeElement.offsetTop0;
let scrollPosition = window.pageYOffset;
if (scrollPosition >= componentPosition && this.scrolledTo === false) {
this.scrolledTo = true;
}
}
HTML:
<div *ngFor="div of divElements; let i = index" [@scrolledToAnimation0]="???" [@scrolledtoAnimation1]="???"></div>
I tried several approaches and none of them seem to work. Any help is appreciated - thanks in advance.
angular angular-animations
so I'm trying to apply different animations for elements in an *ngFor loop. The simplest example would be the following:
@Component({
selector: 'app-anims',
templateUrl: './anims.component.html',
styleUrls: ['./anims.component.css'],
animations: [
trigger('scrolledToAnimation0', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "yellow"
})),
transition('first => second', [
animate('1s')
]),
]),
trigger('scrolledToAnimation1', [
state('first', style({
backgroundColor: "white"
})),
state('second', style({
backgroundColor: "green"
})),
transition('first => second', [
animate('0.40s')
]),
])
]
})
scrolledTo: boolean = false;
divElements: string = ["foo", "bar"];
@HostListener('window:scroll', ['$event'])
checkScroll() {
let componentPosition = this.el.nativeElement.offsetTop0;
let scrollPosition = window.pageYOffset;
if (scrollPosition >= componentPosition && this.scrolledTo === false) {
this.scrolledTo = true;
}
}
HTML:
<div *ngFor="div of divElements; let i = index" [@scrolledToAnimation0]="???" [@scrolledtoAnimation1]="???"></div>
I tried several approaches and none of them seem to work. Any help is appreciated - thanks in advance.
angular angular-animations
angular angular-animations
asked Nov 10 at 17:28
Neekoy
798921
798921
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I figured it out by the way. The variable "index" (or "i" as aliased here) is accessible in the same div it's declared, and can be used to reference each of the *ngFor elements.
It's done in the following way:
<div
*ngFor="let item of Array; let i = index;"
[@animationName]="i === 0 ? 'firstAnimation' : 'secondAnimation'">
</div>
The above references the first element of the array.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I figured it out by the way. The variable "index" (or "i" as aliased here) is accessible in the same div it's declared, and can be used to reference each of the *ngFor elements.
It's done in the following way:
<div
*ngFor="let item of Array; let i = index;"
[@animationName]="i === 0 ? 'firstAnimation' : 'secondAnimation'">
</div>
The above references the first element of the array.
add a comment |
up vote
0
down vote
accepted
I figured it out by the way. The variable "index" (or "i" as aliased here) is accessible in the same div it's declared, and can be used to reference each of the *ngFor elements.
It's done in the following way:
<div
*ngFor="let item of Array; let i = index;"
[@animationName]="i === 0 ? 'firstAnimation' : 'secondAnimation'">
</div>
The above references the first element of the array.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I figured it out by the way. The variable "index" (or "i" as aliased here) is accessible in the same div it's declared, and can be used to reference each of the *ngFor elements.
It's done in the following way:
<div
*ngFor="let item of Array; let i = index;"
[@animationName]="i === 0 ? 'firstAnimation' : 'secondAnimation'">
</div>
The above references the first element of the array.
I figured it out by the way. The variable "index" (or "i" as aliased here) is accessible in the same div it's declared, and can be used to reference each of the *ngFor elements.
It's done in the following way:
<div
*ngFor="let item of Array; let i = index;"
[@animationName]="i === 0 ? 'firstAnimation' : 'secondAnimation'">
</div>
The above references the first element of the array.
answered Nov 11 at 2:43
Neekoy
798921
798921
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241564%2fapply-different-animations-for-elements-in-ngfor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown