random - many divs with same class - different background -
so have many divs same class on same page. add different backgrounds each of them on own choise(having 5-6 random backgrounds)
i found code on internet. chooses same random color divs.
<script type="text/javascript"> /*********************************************** * random content colors script- © dynamic drive dhtml code library (www.dynamicdrive.com) * notice must stay intact legal use * visit dynamic drive @ http://www.dynamicdrive.com/ full source code ***********************************************/ //specify list of random background colors apply css class "randomcolor" //for each entry, can optionally specify text , link color via syntax: // "backgroundcolor:textcolor" or "backgroundcolor:textcolor:linkcolor" var randombgcolors=["green:white:yellow", "#ddf4ff", "#ffff97", "#cfff9f"] var rbcssrule="" var randomnum=math.floor(math.random()*randombgcolors.length) if (randombgcolors[randomnum].indexof(":")!=-1){ rbcssrule="background-color: "+randombgcolors[randomnum].split(":")[0]+";" rbcssrule+="color: "+randombgcolors[randomnum].split(":")[1]+";" } else rbcssrule="background-color: "+randombgcolors[randomnum]+";" document.write('<style type="text/css">\n') document.write('.randomcolor{'+rbcssrule+'}\n') if (randombgcolors[randomnum].split(":").length==3) //if link color specified document.write('.randomcolor a{color:'+randombgcolors[randomnum].split(":")[2]+';}\n') document.write('<\/style>') </script>
it looks code setting randomly chosen color 1 class: randomcolor. assuming of divs have same class, random color.
edit:
check jsfiddle made, should looking for:
html:
<div class="random">div 1</div> <div class="random">div 2</div> <div class="random">div 3</div>
jquery:
$(document).ready(function() { var randomcolors = ["green","yellow","red","blue","orange","pink","cyan"]; $(".random").each(function(index) { var len = randomcolors.length; var randomnum = math.floor(math.random()*len); $(this).css("backgroundcolor",randomcolors[randomnum]); //removes color array can't used again randomcolors.splice(randomnum, 1); }); });
Comments
Post a Comment