0

I'm currently trying to make an simple loader animation that basically increases an element based on a given percentage.

Now i saw that Angular 2 has an animation module and worked around a little bit and this basically only works for static animations.

I've got a loader component that's running an simple interval

var interval = setInterval(function(){
        this.count++;
        this.loading();

        this.circleSize = window.innerWidth / 100 * this.count;

        if(this.count >= 100) {
            clearInterval(interval);
        }

    }.bind(this), 25);

Googling a little further i found angular 2 supports css by variable: [style.width]="circleSize" however this doesn't seem to get updated with the interval.

Now i could a library like GSAP to achieve this but what do you guys recon would be the best/angular approach to do so?

1
  • Well, short answer is to read angular documentation on animations thoroughly and use them correctly. :) What you're doing does not use ng animations at all. For the detailed answer I need a computer, not a phone. :) Commented Dec 2, 2016 at 13:36

1 Answer 1

1

In your component script you can run an interval using:

  constructor: function() {
    this.barwidth = 10;
    setInterval(this.updateCounter.bind(this), 1000);
  },
  updateCounter: function(){
    this.barwidth+=10;
  }

In your template you can change the bar width with:

`<h2>{{ barwidth }} </h2><div class="bar" [style.width]="barwidth">dd</div>`

If you use a transition in your CSS you will see the width of the bar animate:

.bar {
  transition: width 0.5s;
  background-color:green;
  overflow:hidden;
}

For simple animations I would recommend just sticking with CSS!

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

3 Comments

By the way this example is in javascript but it's almost the same in typescript.
Thanks for you answer, got it working now, forgot to add + 'px' behind circleSize, that fixed it.
@LVDM instead of adding 'px', you can also use the binding: [style.width.px]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.