1

I can't make my css transition working when the property to be updated is bind with [ngStyle] or [style.property]

HTML :

<div class="bar" [style.width]="size +'%'"></div>

CSS :

.bar{ width:0; transition: width .5s ease-out; }

JS : size is correctly updated...

To be sure, if i remove the style binding in html and update the width on :hover, transition is working. Do i miss something?

If i let the [style.width] binding and hover it for ex. with a width : 50%!important, transition works. Even though it's not what i want to achieve, it looks like ngStyle block the transitions. And !important is required.

But this is not working either :

[ngStyle]="{'width': size +'% !important'}"

1 Answer 1

1

with this setup, you have to delay binding of size variable,

export class AppComponent{

  constructor(){
     setTimeout(()=>{
        this.size=20;
     },1000)
  }

}
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, transition works on this specific case with your solution. But size in my case is calculated from 2 params inside a bigger object. When i delay the setting of this bigger object it doesn't work. If i set a new property 'percent' for each child on a timeout, transition starts from 0 on each update...
But just wrap size variable within settimeout.
Else you have to use angular2 animation.
Thank you. Well, this is not really handy. Don't understand why it doesn't work on the first place... updating a css property with a transition on it should be straight forward
THat's because angular2 bindings get resolved first and then css gets applied to dom element. so binding first and then css that's the problem. use angular2 animation or you have to delay binding that's it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.