0

I'm building some html thermometers, which are animated with jQuery in order to show represent a percentage number (as you can see in the Fiddle I've built).

What I want, and I can't figure out what I'm doing wrong, is to get the thermometer's color to change depending the level they are representing. For instance in the Fiddle's script those under 80 should be color blue, and those over 80 should be yellow, but all of them turn out blue.

This is the jQuery code:

$('.lvl').each(function(){
    var level = $(this).text();
    $(this).animate( {
        width: level+'%'
    }, 1000);

    var lvlwidth = $(this).width();
    if (lvlwidth < 80) {
        $(this).css('background-color', 'blue');
    } else if (lvlwidth > 80) {
        $(this).css('background-color', 'yellow');
    }
});

Update: As I say in the comments below, what I want is the bar to dynamically change color as it grows, so for example to be blue when it's under 40, then turn yellow, and when it reaches 80 turn red.

5
  • You're checking the width as the animation starts, and at that point the elements have no width. Why aren't you just using the level variable, as you already have the number you want there ? Commented Nov 28, 2014 at 22:15
  • jsfiddle.net/adeneo/rpxr4bjj/1 Commented Nov 28, 2014 at 22:17
  • Thanks @adeneo. I didn't do it that was because I was hoping to get it to change dynamically as it grew, so to be initially one color, change to another once it reaches 40, and then change again when it reaches 80. The level variable is fixed while the width changes. Commented Nov 28, 2014 at 22:27
  • It's possible to do what you wan't, but that would be more complicated and require using the step callback in jQuery's animate Commented Nov 28, 2014 at 22:34
  • "...blue when it's under 40, then turn yellow, and when it reaches 80 turn red" so it should be yellow at 60? Commented Nov 28, 2014 at 23:10

1 Answer 1

1

UPDATE:

http://jsfiddle.net/rpxr4bjj/3/

$('.lvl').each(function () {
    var level = $(this).text();
    $(this).animate({
        width: level + '%',
    }, {
        progress: function () {


            if (level < 80) {
                $(this).css('background-color', 'blue');
            } else if (level > 80) {
                $(this).css('background-color', 'yellow');
            }
        }
    }, 3000);


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

8 Comments

Thanks @dm4web, but don't they all go yellow now? aren't those under width: 80% supposed to stay blue in this example?
i test in FF and works. In chrome they all yellow. and I will look to
I added a correction. problem was the width not Chrome or FF.
Thanks again @dm4web. Do you think there's a way of getting the bars to all start in the same color and change colors as they animate, instead of being all the way the same color?
Like this? jsfiddle.net/fabio_silva/2nbbbcxc/1 (i made some tweaks you may disregard, like a third color level, and transitions)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.