Today, when I was in an organization, I had to display colors at different levels. I had to calculate for a long time but couldn’t figure out the color gradient method. It was always fancy and ugly. So I checked it and found a js code and tried it. It was perfect!
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body><script src="js/jquery-1.11.3.js"></script><script src="js/swiper-3.3.1.jquery.min.js"></script><script type="text/javascript"> /* // startColor: start color hex // endColor: end color hex // step: several levels (several steps) */ function gradientColor(startColor,endColor,step){ startRGB = this.colorRgb(startColor);//Convert to rgb array mode startR = startRGB[0]; startG = startRGB[1]; startB = startRGB[2]; endRGB = this.colorRgb(endColor); endR = endRGB[0]; endG = endRGB[1]; endB = endRGB[2]; sR = (endR-startR)/step;//Total difference sG = (endG-startG)/step; sB = (endB-startB)/step; var colorArr = []; for(var i=0;i<step;i++){ //Calculate the hex value of each step var hex = this.colorHex('rgb('+parseInt((sR*i+startR))+','+parseInt((sG*i+startG))+','+parseInt((sB*i+startB))+')'); colorArr.push(hex); } return colorArr; } // Convert the hex representation to the rgb representation (return the rgb array pattern here) gradientColor.prototype.colorRgb = function(sColor){ var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; var sColor = sColor.toLowerCase(); if(sColor && reg.test(sColor)){ if(sColor.length === 4){ var sColorNew = "#"; for(var i=1; i<4; i+=1){ sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1)); } sColor = sColorNew; } // Process six-digit color values var sColorChange = []; for(var i=1; i<7; i+=2){ sColorChange.push(parseInt("0x"+sColor.slice(i,i+2))); } return sColorChange; }else{ return sColor; } }; // Convert the rgb representation to hex representation gradientColor.prototype.colorHex = function(rgb){ var _this = rgb; var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; if(/^(rgb|RGB)/.test(_this)){ var aColor = _this.replace(/(?:(|)|rgb|RGB)*/g,"").split(","); var strHex = "#"; for(var i=0; i<aColor.length; i++){ var hex = Number(aColor[i]).toString(16); hex = hex<10 ? 0+''+hex :hex;// Ensure that the value of each rgb is 2-bit if(hex === "0"){ hex += hex; } strHex += hex; } if(strHex.length !== 7){ strHex = _this; } return strHex; }else if(reg.test(_this)){ var aNum = _this.replace(/#/,"").split(""); if(aNum.length === 6){ return _this; }else if(aNum.length === 3){ var numHex = "#"; for(var i=0; i<aNum.length; i+=1){ numHex += (aNum[i]+aNum[i]); } return numHex; } }else{ return _this; } } var gradient = new gradientColor('#e82400','#8ae800',10); console.log(gradient);//Console output alert(gradient); for(var i=0;i<gradient.length;i++){ var htmls='<div>'+i+'</div>'; $("body").append(htmls); console.log($("mmm"+i)); console.log(gradient[i]); $(".mmm"+i).css("background-color",gradient[i]); }</script></body></html>The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.