


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
var f=new Array();
f ['0']=0;
f['1']=1;
f['2']=2;
f['3']=3;
f[' 4']=4;
f['5']=5;
f['6']=6;
f['7']=7;
f['8' ]=8;
f['9']=9;
f['A']=10;
f['B']=11;
f['C']= 12;
f['D']=13;
f['E']=14;
f['F']=15;
Because color codes are not case-sensitive, you can convert all colors to uppercase first
code=code.toLocaleUpperCase();//Convert to Capitalize
and then convert hexadecimal to decimal
//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
function colorConversion(code){
var len= code.length;
var f=new Array();
f['0']=0;
f['1']=1;
f['2']=2 ;
f['3']=3;
f['4']=4;
f['5']=5;
f['6']=6;
f['7']=7;
f['8']=8;
f['9']=9;
f['A']=10;
f['B']=11;
f['C']=12;
f['D']=13;
f['E']=14;
f[ 'F']=15;
code=code.toLocaleUpperCase();//Convert to uppercase
var s=code.substr(0,1);
if(s=='#') {
code=code.substr(1,6);
}
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]];
return [r,g,b];
}
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
var _step=10;
var _R_step=parseInt (Math.abs(_thisRGB[0]-_toRGB[0])/_step); //The increase and decrease step size of R
var _G_step=parseInt(Math.abs(_thisRGB[1]-_toRGB[1])/ _step); //The increase and decrease step size of G
var _B_step=parseInt(Math.abs(_thisRGB[2]-_toRGB[2])/_step); //The increase and decrease step size of B
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
var step=10;
var _step=step ;
//In the loop
var s=(step-_step) 1;
_step--;
Then determine whether the current color RGB value and the target RGB value are increasing or decreasing
var r=_step==1?_toRGB[0] :(_thisRGB[0]>_toRGB[0]?_thisRGB[0]-_R_step*s:_thisRGB[0] _R_step*s);
var g=_step==1?_toRGB[1]:(_thisRGB[ 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);
Finally, output the color
obj.css({'background-color':' rgb(' r ',' g ',' b ')'});
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:
/*
Parameters:
obj: target object
thisRGB: 6-digit code of the current background color
toRGB: 6-digit code of the target background color
thisColor: current 6-digit code of text color
toColor: 6-digit code of target text color
step: number of executions
speed: execution speed
*/
function colorGradient(obj,thisRGB,toRGB,thisColor ,toColor,step,speed){
var _thisRGB=colorConversion(thisRGB); //Convert hexadecimal to decimal
var _toRGB=colorConversion(toRGB);
if(thisColor&&toColor){
var _thisColor=colorConversion(thisColor,1);
var _toColor=colorConversion(toColor,1);
}
var step=step?step:3;
var _step=step;
var _speed=speed?parseInt(speed/step):30; //Calculate the speed of each execution based on the total time
var _R_step=parseInt(Math.abs(_thisRGB[0]-_toRGB[0]) /_step);
var _G_step=parseInt(Math.abs(_thisRGB[1]-_toRGB[1])/_step);
var _B_step=parseInt(Math.abs(_thisRGB[2]-_toRGB[2 ])/_step);
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
