Creating a transition when changing properties in angular 2/4

I want to create a transition effect when I change the value of a property.

I tried to do the following

    @Component({   
    selector: 'image-holder',
    template: `
        <div class="carousel-image">
            <img src="{{ image }}" [@slideInRight]="slide" />
            <span>{{ text }}</span>
        </div>
    `,
    styleUrls: ['../greenscreen.scss'],
    animations: [
        trigger(
            'slideInRight',
            [
                transition(
                ':enter', [
                style({transform: 'translateX(100%)', opacity: 0}),
                animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
                ]
            ),
            transition(
                ':leave', [
                style({transform: 'translateX(0)', 'opacity': 1}),
                animate('500ms', style({transform: 'translateX(100%)',opacity: 0}))
                ]
            )
            ])
    ]
})
export class ImageHolderComponent implements OnChanges {
    @Input()
        image: string;
    @Input()
        text: string;

    public slide: boolean = true;

    public ngOnChanges(changes: { [propKey: string]: SimpleChange }){
        this.slide = !this.slide;
    }
}

I assumed that changing the property will cause the component to start the animation effect again, but this does not work as expected

+4
source share
2 answers

You can use * ngFor for these use cases, even if it is only one object.

@Component({
  selector: 'image-holder',
  template: `
    <div class="carousel-image">
      <img [src]="image" *ngFor="let image of [image]" [@slideInRight]/>
      <span>{{ text }}</span>
    </div>
  `,
  styleUrls: ['../greenscreen.scss'],
  animations: [
    trigger(
      'slideInRight',
      [
        transition(
          ':enter', [
            style({transform: 'translateX(100%)', opacity: 0}),
            animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
          ]
        ),
        transition(
          ':leave', [
            style({transform: 'translateX(0)', 'opacity': 1}),
            animate('500ms', style({transform: 'translateX(-100%)', opacity: 0}))
          ]
        )
      ])
  ]
})
export class ImageHolderComponent {
  @Input()
  image: string;
  @Input()
  text: string;

}
+3
source

this.slide =! this.slide will be true and false but when I look at your trigger you say [@slideInRight] = "slide"

so basically true or false.

but when I look in your animations, I see: enter and: leave

"true" "false" 1 0

, slideInRight , true false.

    trigger('work', [
  state('*', style({transform: 'translateX(0)', opacity: 1})),
  state('true', style({'border-right': '50px solid #72BF44', opacity: 1})),
  state('false', style({'border-right': '20px solid #72BF44', opacity: 1})),
  transition('1 => 0', animate('.125s .1s cubic-bezier(0.29, 0.06, 0.43, 0.92)')),
  transition('0 => 1', animate('.125s 0s cubic-bezier(0.29, 0.06, 0.43, 0.92)')),
  transition('void => *', [
    style({transform: 'translateX(-100%)', opacity: 0}),
    animate('1s 1.1s cubic-bezier(0.29, 0.06, 0.43, 0.92)')
  ])
])

, .

// , ( ..).

, ... true false false true void true, , .

0

Source: https://habr.com/ru/post/1667944/


All Articles