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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 17:28









      Neekoy

      798921




      798921
























          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.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 2:43









                Neekoy

                798921




                798921






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues