jquery - Change css background color of body, everytime button is clicked -
i webpage background color change between few colors (possibly more have input here) every time button clicked. here code have far:
var colors = ["rgba(86,175,99,0.6)", "rgba(86,175,222,0.6)", "rgba(0,0,0,0.6)"]; var randomcolor = colors[math.floor(math.random() * colors.length)]; jquery(".rotator").click(function () { jquery('body').css("background", randomcolor); });
it seems code not repeat itself. stuck how this.
thanks
your random color generation execute once because declare variable outiside of click event. generate random color inside click event.
jquery(".rotator").click(function() { var randomcolor = colors[math.floor(math.random() * colors.length)]; jquery('body').css("background", randomcolor); });
Comments
Post a Comment