|
||||||
How to Add Christmas Tree Lights to a Web SiteA Javascript Function for Animating Christmas Decorations
Every Christmas tree needs flashing lights - and with Javascript that Christmas tree and its decorations can be on a web site.
One of the iconic images of Christmas is the Christmas tree: there's one in every town center, every shop window, and every home - each one decorated in baubles and flashing lights; and, of course, every web site should have it's own Christmas tree as well. A Christmas tree on a web page can take one of two forms:
A Static Christmas TreeBoth the animated and the static Christmas trees start with the same images:
The images can now be combined in a web page, however they will not be laid out as normal on web page (i.e. side by side or one above the other), instead:
This is actually done very easily by modifying the style of each image - the style is used to define how an object (such as an image) looks and behaves, and has some useful properties:
The end result is an image of a tree: <body>
<img id=tree src=tree.jpg
style=position:absolute;z-index:0>
Overlaying the tree will be the decorations<:/p> <img id=red src=red.png
style=position:absolute;z-index:1;left:96;top:225>
<img id=red src=red.png
style=position:absolute;z-index:1;left:320;top:225>
<img id=blue src=blue.png
style=position:absolute;z-index:1;left:200;top:50>
<img id=blue
src=blue.png style=position:absolute;z-index:1;left:300;top:150>
<img id=yellow src=yellow.png
style=position:absolute;z-index:1;left:229;top:157>
<img id=yellow src=yellow.png
style=position:absolute;z-index:1;left:200;top:305>
And overlaying both the tree and the decorations will be any web content: <div style=position:absolute;z-index:2>
<h1>O Christmas Tree</h1>
O Christmas Tree! O Christmas Tree!<br>
Thy candles shine so brightly!<br>
O Christmas Tree! O Christmas Tree!<br>
Thy candles shine so brightly!<br>
From base to summit, gay and bright,<br>
There's only splendor for the sight.<br>
O Christmas Tree! O Christmas Tree!<br>
Thy candles shine so brightly!<br>
</div>
</body>
Making the Christmas Tree Lights Flash by Using JavascriptThe technique for making the Christmas tree lights turn on and off is quite simple:
The script starts by creating an array containing a list of colors to be used: <script>
function flash_lights () {
var colors = new Array ("red", "blue", "yellow");
One of these colors is then randomly selected: var rand_no = Math.random();
rand_no = parseInt (rand_no * colors.length);
color = colors[rand_no];
The script then loops through all of the images on the page and hides any that have an id equal to the randomly selected color: for (var i=0;i<document.images.length;i++) {
document.images[i].style.visibility = "visible";
if (document.images[i].id == color) {
document.images[i].style.visibility = "hidden";
}
}
Finally the function resubmits itself - giving itself a one second (1000 milliseconds) delay: setTimeout ('flash_lights()', 1000 );
}
Of course, the function must be called from the Javascript code in order for the lights to start flashing: flash_lights ();
</script>
ConclusionThis is simple technique that can be implemented very quickly, and it can easily be adapted to other types of displays - for example:
And the key is having just the right images and a single Javascript function.
The copyright of the article How to Add Christmas Tree Lights to a Web Site in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Add Christmas Tree Lights to a Web Site in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||