Home >Web Front-end >JS Tutorial >Javascript color gradient effect implementation code_javascript skills
The following are some ideas and solutions from bloggers. If you are not interested in this and want to use the jquery plug-in directly, you can click here
Thoughts
Each color is composed of RGB, and each two digits is a hexadecimal number
After converting the current color code and the target color code into decimal numbers, there is a difference. Using the difference, set Determine the step size of the total number of executions, and calculate the decimal number for changing the color at each step
Use the timer to execute
To put it simply, convert the 6-digit color code into a decimal number for every two digits, and then Calculate the difference between two pairs of RGB values. According to the set step size (number of executions), calculate the RGB value that needs to be increased or decreased at each step, and finally become the RGB value of the target color
Problems that need to be solved
Convert the 6-digit color code to decimal
Calculate the increasing or decreasing value at each step according to the step size
Use a timer to perform this increasing or decreasing process
1. Convert the 6-digit color code It is decimal
About converting hexadecimal to decimal, it has already been discussed in school textbooks. The ones digit * 16 raised to the 0th power, the tens digit * 16 raised to the 1st power, and so on. Color is composed of RGB, and each two digits are a group, such as: #123456, R=12, G=34, B=56, but in fact the RGB value is decimal, so R=12 can only be said to be corresponding position, 12 is converted to decimal: 2*1 1*16=18, 34: 4*1 3*16=52, 56: 6*1 5*16=96, so RGB=[18,52,96 ].
This is digital, but there are A-F in hexadecimal, so you have to convert A-F to 10-15 first. You can first use an array to save the entire hexadecimal corresponding number
Because color codes are not case-sensitive, you can convert all colors to uppercase first
//code is the 6-digit color code, such as: f07786;
var r=f[code[0]]*16 f[code[1]];
var g=f[code[ 2]]*16 f[code[3]];
var b=f[code[4]]*16 f[code[5]];
The entire conversion code is written as a method
The s in the code is used to determine whether the color code has a # sign. If so, remove it, and finally return an array containing RGB values
Calculate the increasing or decreasing step size
For example, if you set the number of color changes to 10, you need to calculate the increase or decrease in the RGB value for each of these 10 changes. Use the absolute value of the difference between the RGB value of the current color and the RGB value of the target color, and then divide it by 10 to get a step size, but this value is likely to be a decimal. You can round off the decimal, then increase or decrease the value in the last step When the time comes, just change it directly to the RGB value of the target color
Increase or decrease each execution
If the number of executions is 10, it means that it needs to be executed 10 times continuously. When _step=1, the execution is completed. Then when increasing or decreasing the step size, it will appear. If _step=10, then the increase or decrease is 1 times the step size. If _step=9, that is, the execution reaches the second step, then the increase or decrease is 2 times the step size. , until _step=1, increase or decrease the step size by 9 times. Here you can use such a simple calculation
Then determine whether the current color RGB value and the target RGB value are increasing or decreasing
Finally, output the color
The output here is the rgb() method. It doesn’t matter. It’s the same as the color code. If you still want to output a 6-digit code, just convert the decimal to hexadecimal
The last step is to use a timer to execute. There is also speed and calculation in the middle, which I won’t go into here. The final execution code:
var timer=setInterval(function(){
if(_step>0){
var s=(step-_step) 1;
var r=_step==1?_toRGB[0] :(_thisRGB[0]>_toRGB[0]?_thisRGB[0]-_R_step*s:_thisRGB[0] _R_step*s); 1]>_toRGB[1]?_thisRGB[1]-_G_step*s:_thisRGB[1] _G_step*s);
var b=_step==1?_toRGB[2]:(_thisRGB[2]> _toRGB[2]?_thisRGB[2]-_B_step*s:_thisRGB[2] _B_step*s);
obj.css({'background-color':'rgb(' r ',' g ',' b ')'});
through :_thisColor[0] _R_step*s);
var cg=_step==1?_toColor[1]:(_thisColor[1]>_toColor[1]?_thisColor[1]-_G_step*s:_thisColor[1 ] _G_step*s);
var cb=_step==1?_toColor[2]:(_thisColor[2]>_toColor[2]?_thisColor[2]-_B_step*s:_thisColor[2] _B_step*s );
obj.css({'color':'rgb(' cr ',' cg ',' ' cb ')'});
clearInterval(timer);
return true;
}
},_speed);
}
This method is very simple, but the smoothness of the gradient is average, especially when a group of objects are executed continuously. All I can say is that this is a very clumsy and stupid method. The great masters all use the Tween algorithm
To use it, just use jquery's animate directly. You just don't need to specify the current color. The program will automatically take the current color, but you must set the background in the style