In this article, I will show you how to use Java Script to create an animated banner. Using Java Script allows you to edit or replace images in the animation sequence, and change the amount of time each image is displayed without doing any recompiling.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Easy Animated Banners with Java Script

Animated banners are usually animated gif images. But there are several problems with animated gifs. First, they consist of several gif images compiled into a single file. To make any changes to the banner you must retrieve the original images and recompile the animated gif. Second, you must use .gif images, you can't use .jpg images. And third, the entire animated gif file must be downloaded before any animation can begin.

In this article, I will show you how to use Java Script to create an animated banner. Using Java Script allows you to edit or replace images in the animation sequence, and change the amount of time each image is displayed without doing any recompiling.

The first thing you need to create any animated banner is the sequence of images of which the animation will be composed. For this example, I use four images that create an animated banner advertising free ebooks on this Web site.

Create a Java Script code block in the head of your Web page that preloads your images as shown below.

<script language="JavaScript">
var image1 = new Image;
var image2 = new Image;
var image3 = new Image;
var image4 = new Image;
image1.src = "frame1.gif";
image2.src = "frame2.gif";
image3.src = "frame3.gif";
image4.src = "frame4.gif";
</script>

Inside that same code block, define three variables as shown below.

var frame =
new Array(image1,image2,image3,image4);
var numframes = frame.length;
var curframe = 0;

The variable frame places all the images in an array. The variable numframes recieves the number of images in the array. The variable curframe will contain the array index of the currently displayed image.

The image tag below is placed within the body of the web page to display the banner.

<img alt="free ebooks!" border=0 width=468 height=60 name="myBanner" src="frame1.gif" onClick="window.location.href='targetpage1.htm'">

There are two important attributes of this image tag to note. First, the image html element is given the name myBanner. This is the name we will use to refer to it in the Java Script code. Second, when the user clicks on the banner image, the new page targetpage1.htm is loaded into the browser window.

The Java Script function shown below performs the banner animation. This code is added to the Java Script code block in the head of your Web page.

function animateBanner()
{
   curframe++;
   if(curframe == numframes) curframe = 0;
   document.myBanner.src = frame[curframe].src;
   setTimeout("animateBanner()",1200);  
}

The line curframe++ increments the value in curframe, the array index, by one. The next line checks to see if the index is the same as the number of images, if it is, curframe is reset to 0. The next line loads an image from the frame array into the banner image, replacing the previous image. The last line sets up a timer that will call the function again in 1200 milliseconds.

The only thing left to do is call the animateBanner function from the onLoad event in the body tag of your Web page, as shown below.

<body onLoad="animateBanner()">

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269