logo Send us mail:
contact@reliancewisdom.com
WhatsApp or call:
+234 (0)808 881 1560
TOGGLE NAVIGATION
Back to JavaScript Programming
How to Create Animated Images with HTML and JavaScript
Ishola Ayinla    May 25, 2017 at 07:59 AM    1    894
I have a project at hand and I need to create animated images on the home page. With this, the images will be changing after some seconds. Please, how can I do this with HTML and JavaScript?
Back to JavaScript Programming

1 Answer
Ishola Wasiu says...
May 25, 2017 at 10:13 AM

We've all heard of animated gifs and most of us would have made some for our web sites. Such gifs can be created using many commercial and free programs. The animated gifs are a series of images (frames) that are played sequentially giving the semblance of motion/change. The Gif89a gif format supports animation along with transparency. The jpg image format has no provision for animation, however, with a little JavaScript and a dash of creativity, we can easily make an "animated" jpg. You should keep in mind that the animated jpg is not one image but a series of images that are displayed in the same place in the browser. We start by preloading all the jpg images required in the "animation". The second step is to use a function that changes the source of an image. Finally, we call this function repeatedly by employing the setTimeout() method.

Open a new file and save it as "animated_jpg.htm" then place the following script inside HEAD section of the HTML document:

<script type="text/javascript" > 
<!-- 
var c = 1; 
/* Preloading images */ 
var image1 = new Image(); 
image1.src = "a1.jpg"; 
var image2 = new Image(); 
image2.src = "a2.jpg"; 
var image3 = new Image(); 
image3.src = "a3.jpg"; 
var image4 = new Image(); 
image4.src = "a4.jpg"; 
var image5 = new Image(); 
image5.src = "a5.jpg"; 
function disp_img(w) 
   { 
   if (c == 6) 
      {      c = 1; 
      } 
   var img_src = "a" + c + ".jpg"; 
   document.ani.src = img_src; 
   c++; 
  } 
t = setInterval("disp_img(c)", 1000); 
//--> 
</script>

We start by initializing a global variable c that serves as a counter and is also a part of the image name. After preloading the images (a1.jpg, a2.jpg, a3.jpg, a4.jpg and a5.jpg), we define a function disp_img() that changes the source of images named ani. The function also checks the value of variable c, assigning it a value of 1 if its value exceeds 5. The variable is also incremented each time the function is called. The function is called after every 1 second (1000 milliseconds) with the setInterval() method.

Then type in the BODY section:

<img src="a5.jpg" width="150" height="86" name="ani" alt="'Animated' Jpg" />

You can customize the script and create .jpg slide-shows easily.

Note: These "animated" jpgs will not work on browsers that have JavaScript disabled.

Also Note: You must have five(5) images named "a1.jpg", "a2.jpg", "a3.jpg", "a4.jpg" and "a5.jpg" repectively, in the same directory(folder) you saved the "animated_jpg.htm" file if you realy want the above code to function acurately as expected.


Full Details
Add a friend to chat
Hi ! You have not added any friend to chat with. Kindly click on the link below to add a friend. Add a friend