Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

How to change Angular animation duration from code

I am trying to figure out how to dynamically change the duration of an angular animation based on a button click. I'd like to be able to set a button for slow, medium, and fast which would update the '600ms ease' to different durations but I cant successfully set it to a variable. I tried setting a variable named speed like this:

transition('rest =>right', animate(this.animationSpeed)),

but I get a warning that the "Object is possibly undefined"

Here is my animation trigger. Has any one dealt with this issue before?

animations: [ trigger('moveItem', [

  state('right', style ({ 
    transform: 'translateX(45vw)',
  })),
  state('left', style ({ 
    transform: 'translateX(-45vw)',
  })),
 
  transition('rest =>right', animate('600ms ease')),
  transition('right =>left', animate('600ms ease')),
  transition('left =>right', animate('600ms ease')),
  transition('rest =>left', animate('600ms ease')),
  transition('left =>rest', animate('600ms ease')),
  transition('right =>rest', animate('600ms ease')),

Thank you for your time!

question from:https://stackoverflow.com/questions/65599401/how-to-change-angular-animation-duration-from-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you're trying to do isn't possible for the moment, angular doesn't accept changing the duration with parameters

This may be a workaround

Bind it with css in the component.html file

<img src="anUrl"
   [ngStyle]="transition"
/>

Then switch the animation within the component.ts

translation = {}

onRight(duration: string): void {
  translation = {
   'transition-duration.ms': duration,
   transform: 'translateX(45vw)',
 }
}


onLeft(duration: string): void {
  translation = {
   'transition-duration.ms': duration,
    transform: 'translateX(-45vw)',
 }
}

dont forget to add the default transition within the component.scss file

img {
  transform: translate(0,0)
}

It's surely not not the perfect solution, but it may help


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...