A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Animated Color MatrixThis tutorial, especially recommended to beginners, will give you a better understanding of the JavaScript language: how to use loops, arrays and functions to create a fun animation. |
On Saturday, February 10th 2007 at 07:48 PM By Sorin Sodolescu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.9 with 7 votes) |
||
|
Contextual Ads
More JavaScript Resources
Advertisement
![]() This JavaScript will make the background color of a few DIV objects change by creating animation patterns. Let's start by creating the HTML that will later be manipulated by the JavaScript function. First, we create a master DIV: <div id="masterDiv" style="position: relative; width: 120px;"> This will contain 9 other divs that we will use for the animation: <div id="1" style="position:relative; width:30px; height:30px; background-color:red; float:left; margin:5px"></div> And then we close the master div: We have to set the event that will trigger the function that we're about to declare; to make the function start when de page loads we'll specify it in the body tag. <body onload="animate()"> I've used CSS to set the div's background-color property in such a way that it creates a pattern; the lower row's divs are offset by 1 div. We are now ready to write the JavaScript function.Because we want to use the function to change the div's background color into a certain color, we will have to use the If/Else If/Else statement. I will associate a variable "a" with the matrix's state (for the default state, "a" will be 0). The animation will play over and over, because the function contains a loop, so we have to declare the "a" variable outside of the function, otherwise every time the function will be called, "a" will be assigned to 0. <script type="text/javascript"> Now, we have to declare and initiate some arrays with all the possible color combinations a row can have. colors= new Array() We also have to declare 3 more variables (the number of the first div on each row), variables that we will use to identify the divs when changing colors. var b=1 When changing colors we have to know what's the current patern, this is when we use the a variable. We know what pattern the matrix has when the page loads so we can now write how the colors will change. if (a==0) To change the color of all of the divs, we have to increment the b, c and d variables, so all of the IDs will have declared properties. b++ Now, to avoid entering in this loop again, we increment the "a" variable by 1. Next we use the setTimeout() method to make the function loop every 0.5 seconds so we can set the other patterns. a++ At this point "a" equals 1, so we will write the code for the next pattern in the Else If statement. This part is just like the previous one, except now we will assign the arrays offset by one. else if (a==1) We have to do this one more time in order to get the desired pattern. This time a=2, but since this is the last statement we can just an Else statement. This time we'll assign a the 0 value, so the function will start over. else The script is now complete. If you want to see how this works, just copy the code below: <html> if (a==0) <body onload="animate()"> |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current Commentsverey bad
verey bad
Related Tutorials
Related Source Code
JavaScript Job Search