Zihark
Zihark

Reputation: 21

Controlling Color Changes

I want to have a button change a color after a certain amount of time and fade back to normal while using arrays to control it. i have done this so far:

<input id="q" type="button" value="Q" style="width:50px;height:50px;color:#ffffff;background-color:#5142F5;border-color:#1BE0DD"/>


<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript">
var array[0,1,0];

setTimeout(
    function(){
      array.splice(0,1,1)
      switcher()
    }, 600);

function switcher(){
switch(array[0]){
case 0:
    $('#q').css('background-color', '#5142F5');
    break;
case 1:
    $('#q').animate({backgroundColor: "#F8FF2B"},400);
    setTimeout(
    function(){
      $('#q').animate({backgroundColor: '#5142F5'},400);
    }, 600);
    setTimeout(
    function(){
      array.splice(0,1,0)
    }, 1000);
    break;
}
}
</script>

but it doesnt work for some reason. am i missing something?

Upvotes: 0

Views: 70

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

jQuery animate does not support background-color animation for that you need to include the color plugin

From Docs

but background-color cannot be, unless the jQuery.Color() plugin is used).

Try

var array = [0,1]

setInterval(switcher, 1500);

function switcher(){
    var val  = array.splice(0, 1)[0];
    switch(val){
        case 0:
            $('#q').stop(true, true).css('background-color', '#5142F5');
            break;
        case 1:
            $('#q').stop(true, true).animate({backgroundColor: "#F8FF2B"}, 400);
            break;
    }
    array.push(val);
}

Demo: Fiddle

Upvotes: 2

Ivan Chernykh
Ivan Chernykh

Reputation: 42166

Seems like you have error in array definition:

var array[0,1,0]

must be:

var array = [0,1,0];

Or may be it is only "typo"

Upvotes: 1

Related Questions