Dante’s blog

March 17, 2010

Images rotator with javascript

Filed under: HTML, Java Script — Tags: , — Dante @ 1:41 pm

A little “javascript” to add in a HTML page an images rotator.


var images = new Array(10);
var index = 0;
function load_images(the_path, the_prefix, images_count)
{
var image_path_name = the_path + the_prefix;
var image_name;
var i = 1;
for (i = 1; i <= parseInt(images_count); i ++)
{
if (i < 10)
{
image_name = image_path_name + '0' + i + '.jpg';
}
else
{
image_name = image_path_name + i + '.jpg';
}
images[parseInt(i)-1] = image_name;
}
setInterval("Show_Image()",5000);
}
function Show_Image()
{
index = index + parseInt(1);
if (index > 9)
{
index = 0;
}
My_Image = document.getElementById("the_splash");
My_Image.src = images[index];
}

The function takes 3 parameters:

1) The path where the images are stored, for example “images/”.
2) The prefix of the images, for example. “gira”. The prefix of the images is used to group all the images that will rotate.
3) The number of images to display, for example “10″.

The BODY section of the HTML code to use the script:


<body onload="load_images('images/','gira',10)">
<div id="splash">
<asp:Image runat="server" ID="the_splash" />
</div>
</body>

Marchesi Dante

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.