|
||||||
How to Create an Advent Calendar with JavascriptCount Down the Days before Christmas on a Web Site
Millions of children will be opening a door on an Advent calendar throughout December as Chistmas approaches. Make that a web page with just a few images and Javascript
The Advent calendar, as we know it, has existed since the 19th century, and has been used by generations of children; however children in the 21st century are as (if not more) likely to be looking for a cyber Advent calendar as they are a cardboard one. However, that's not a problem because a web site developer can easily add an Advent calendar on to a web page - by using Javascript. However, before starting programming it's worth considering exactly what the Advent calendar will do:
Creating Pictures for the Advent CalendarThe pictures for the Advent Calendar are traditionally in a Christmas theme, for example:
Obviously these pictures can be as detailed as required, but for simple pictures icon editors such as KIconEdit will do the job nicely (for more information on creating and using icons have a read of How to Use Javascript to Animate Snowflakes). Placing the Pictures on a Web PageEvery web site designer will have played images on a web page, and there are a few ways to to this:
In this case the style option is the most useful because both the 'door' and the picture that it hides must occupy the same space. With that in mind it's time to start thinking about the Javascript code. Javascript Code for an Advent Calendar Web PageIn this example a multidimensional array is used to store the coordinates of each Advent calendar item - the coordinates will be a pair of values representing the image's top and left properties. The Javascript will also need to provide functions that will:
A Javascript Multidimensional Array for Storing CoordinatesThe coordinate array is simple an array with 24 elements (the number of days) each element of which is another array with 2 more elements ( the top and the left of the image to be displayed): var coords = new Array();
coords[1] = new Array(100,100);
coords[2] = new Array(140,220);
Obviously, coordinates for days 3 to 22 will be required as well coords[23] = new Array(100,140);
coords[24] = new Array(220,60);
A Javascript Function to Calculate the Current DayThe function for calculating the current must take the following into account:
function current_day () {
var today = new Date();
var month = today.getMonth() + 1 ;
if (month == 11 ) {
return today.getDate();
} else {
return 0;
}
}
Javascript Function to Open and Close Advent Calendar DoorThe functions for opening and closing the doors use the current_dayfunction to decide which door may be opened and then hide or reveal the appropriate images: function open_door (day) {
if (day == current_day()) {
var door = document.getElementById("door" + day);
door.style.visibility = "hidden";
var present = document.getElementById("present" + day);
present.style.visibility = "visible";
}
}
function close_door (day) {
if (day == current_day()) {
var door = document.getElementById("door" + day);
door.style.visibility = "visible";
var present = document.getElementById("present" + day);
present.style.visibility = "hidden";
}
}
Laying out an Advent Calendar with JavascriptA simple Javascript loop can now be used to place each of the images in its correct location: for (var i=1;i<=24;i++) {
document.write(
"<img id=present" + i +
" src=" + i + ".png style=position:absolute;top:" +
coords[i][0] + ";left:" + coords[i][1] +
";z-index:1;visibility:hidden " +
"onmouseout='close_door(" + i + ");'>");
document.write(
"<div id=door" + i +
" style=position:absolute;top:" + coords[i][0] +
";left:" + coords[i][1] +
" onmouseover='open_door(" + i + ");' >" +
"<b style=z-index:1;position:absolute>" + i + "</b>" +
"<img src=door.png " +
"style=position:absolute id=innerdoor" + i + "></div>"
);
}
Opening Advent Calendar Doors from Earlier DaysAnd finally any door from previous days in the month can be opened automatically - again by using a simple loop: for (var d=1; d < current_day(); d++){
var present = document.getElementById("present" + d);
present.style.visibility = "visible";
var innerdoor = document.getElementById("innerdoor" + d);
innerdoor.style.visibility = "hidden";
}
ConclusionThe Javascript code required to create an Advent calendar for a web page is very simple - but the end result is very effective; and simple changes (such as changing the images or the image coordinates) mean that the same basic code can be repeated to give different Advent calendars on different web pages.
The copyright of the article How to Create an Advent Calendar with Javascript in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Create an Advent Calendar with Javascript in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||