There can be any number of divs in my project, such as one thousand, two thousand, one million, etc. I want their background colors to change from green to red. therefore, they all acquire different shades of color. the first div will be "real" green, the last div will be "real" red.
Here is what I have. As you can see, at the end there are divs that are left without a background color. I would prefer to solve this problem with rgb.
$(function(){ var r = 20; var g = 200; var b = 10; for(var i = 0; i < 300; i++){ $("body").append("<div class = 'box'>"); } $(".box").each(function(){ if(g > 0 && r < 255){ $(this).css("background", "rgb("+ r + ","+ g + ","+ b + ")"); g-=1; r+=1; } }) })
.box{ border:2px solid black; margin: 10px; width: 20%; height: 100px; float: left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
javascript jquery html css
jack blank
source share