Change the color of a div with a setInterval

Asked

Viewed 992 times

0

Here is the code:

 
jQuery(function($) {
    $('a.panel').click(function() {
		console.log($($(this).attr('href')));
        var $target = $($(this).attr('href')); 		
            $target.show().css({
                left: -($target.width())
            }).animate({
                left: 0
            }, 500);       
    });
});
#right {
    position: relative;
    float: left;
    margin: 0 5px 0 0;
    border: 1px solid black;
    width: 200px;
    height: 200px;
    overflow: hidden;
}

div.panel {
    position: absolute;
    height: 100%;
    width: 100%;
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#target1" class="panel">Target 1</a><br/>

<div id="right">
    <div class="panel" id="target1" style="background:blue">
        <script>
            var tmp = new Array("1","2","3","4");
			var myvar;
			var i = 0;
							
			var timer = setInterval(function() {
				if(tmp.length) {
					var p= tmp.shift();
					console.log(p);
					document.getElementById("right").innerHTML = p;
				} else {
					clearInterval(timer);
				}
			}, 5000);		 		 
		</script>
    </div>   
</div>

My goal is whenever a number appears the background changes color... So far I’ve only found functions that do it separately. I’ve tried to change the click but to no avail.

  • You can use jQuery?

  • @Ana, consider putting the code in the question and not giving a link with it

  • @Felipeavelar Her code already uses jQuery.

2 answers

1

I think this is what you’re looking for:

var tmp = new Array("1","2","3","4");
var color = new Array("red", "green", "blue", "yellow");
var myvar;
var i = 0;
							
setInterval(function() {
    $( "#right" ).text(tmp.shift());
    $( "#right" ).css("background-color", color.shift());
}, 5000);
#right {
    position: relative;
    float: left;
    margin: 0 5px 0 0;
    border: 1px solid black;
    width: 200px;
    height: 200px;
    overflow: hidden;
}

div.panel {
    position: absolute;
    height: 100%;
    width: 100%;
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="right">
</div>

  • that’s right, thank you!

  • there is way to preserve the sliding effect to the left whenever it changes color? .css({&#xA; left: -($target.width())&#xA; }).animate({&#xA; left: 0&#xA; }, 500);

1

Taking the example given, this must be what you want.

<a href="#target1" class="panel">Target 1</a><br/>

<div id="right">
    <div class="panel" id="target1" style="background:blue">
        <script>
            var tmp = new Array("1","2","3","4");
            var clr = new Array("red","blue","green","yellow");
            var myvar;
            var i = 0;

            var timer = setInterval(function() {
                if(tmp.length) {
                    var p= tmp.shift();
            var c = clr.shift();
                    console.log(p);
                    document.getElementById("right").innerHTML = p;
document.getElementById("right").style.backgroundColor =  c;
                } else {
                    clearInterval(timer);
                }
            }, 500);                 
        </script>
    </div>   
</div>
  • there is way to preserve the sliding effect to the left whenever it changes color? .css({&#xA; left: -($target.width())&#xA; }).animate({&#xA; left: 0&#xA; }, 500);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.