Recent Posts

This Post Has Been Viewed 415 Times.

Simple Javascript Slideshow

Posted by admin | Posted in JavaScripts | Posted on 18-03-2009

0

Here i will explain how to create a simple slideshow using javascript.

Our main function displaynext(shift) will receive the value of shift based on the navigational buttons clicked ( it will either +1 or -1 ) and it will receive the value of 0 at the time of page load. Inside the function first the value of present slide is changed after adding the value of shift with present slide value. Then by one if condition the value of slide is checked as it should be within the values of image array. The value should be between 0 and number of element in the array. Once the if condition is satisfied, by using the .src property of the image element of the document the image is displayed. In the next line we will download the next image to be shown at background to cache. This way the next image can be shown immediately without any time delay. Here is the code of displaynext function.

function displaynext(shift){
present_slide=present_slide + shift;

if(images.length > present_slide && present_slide >= 0){
document.images[''im''].src = images[present_slide];

var next_slide=present_slide + 1;
download(next_slide); // Download the next image
}

At the time of page load we will pass a value of 0 to displaynext function to display the first element of the image array. While displaying this first element since the present slide value will be 0 so the previous button will not be visible. Here is the complete code of the slide show page.

    1. <html>
    2. <head>
    3. <title>(Type a title for your page here)</title>
    4. <script language=”JavaScript”>
    5. var present_slide=0;
    6. var images = new Array(”http://www.anyserver/image//2435.jpg”,
    7. ”http://www.anyserver/image//2436.jpg”,
    8. ”http://www.anyserver/image//2437.jpg”,
    9. ”http://www.anyserver/image//2438.jpg”,
    10. ”http://www.anyserver/image//2439.jpg”,
    11. ”http://www.anyserver/image//2440.jpg”,
    12. ”http://www.anyserver/image//2441.jpg”,
    13. ”http://www.anyserver/image//2442.jpg”);
    14. objImage = new Image();
    15. function download(img_src){
    16. // preload the image file
    17. objImage.src=images[img_src];
    18. }
    19. function displaynext(shift){
    20. present_slide=present_slide + shift;
    21. if(images.length > present_slide && present_slide >= 0){
    22. document.images[''im''].src = images[present_slide];
    23. var next_slide=present_slide + 1;
    24. download(next_slide); // Download the next image
    25. }
    26. if(present_slide+1 >= images.length ){
    27. document.f1.Next.style.visibility = “hidden”;
    28. present_slide=images.length-1;
    29. }else{document.f1.Next.style.visibility = “visible”;}
    30. if(present_slide<=0 ){
    31. document.f1.Prev.style.visibility = “hidden”;
    32. present_slide=0;
    33. }else{
    34. document.f1.Prev.style.visibility = “visible”;}
    35. }
    36. </script>
    37. </head>
    38. <body onLoad=”displaynext(0)”>
    39. <form name=”f1″ method=post action=””>
    40. <img name=”im” ></a><br>
    41. <input type=”button” name=”Prev” value=” << ” onClick=”displaynext(-1);”>
    42. <input type=”button” name=”Next” value=” >> ” onClick=”displaynext(1);”>
    43. </form>
    44. </body>
    45. </html>

      VN:F [1.5.7_846]
      Rating: 5.5/10 (2 votes cast)
      VN:F [1.5.7_846]
      Rating: -1 (from 1 vote)
      Share and Enjoy:
      • Digg
      • Sphinn
      • del.icio.us
      • Facebook
      • Mixx
      • Google
      • De.lirio.us
      • Xerpi

      Related posts:

      1. Caching of images using javascript image object
      2. Slideshow using jquery
      3. Simple Calendar Script Using Javascript
      4. Windows XP shortcuts for interner explorer, excel and powepoint
      5. Adding a draggable div along with the cursor using simple javascript
      6. XML Parsing Made Easy
      7. Adding and removing child nodes using javascript and DOM

      Write a comment

      Spam Protected