www.webdeveloper.com
Results 1 to 7 of 7

Thread: JS change image 3 sec for 3 different images from 3 different image folders?

  1. #1
    Join Date
    Feb 2012
    Posts
    23

    JS change image 3 sec for 3 different images from 3 different image folders?

    Hey guys first off I want to let you know that I am a total new noob in JS, I only know html so detail will be necessary.

    Ok my website HOME PAGE has 3 images on the side. I want all 3 of those images to change every X seconds, each has to change from a different folder, image1 /path/SMLpics. image2 /path/LRGpics. image3 /path/XLpics.

    Also I need to be able to change the frequency of all 3, they cant all change at say 3 seconds. I may want one to do 1sec, second to do 5 sec, and third to do 10 but I need to be able to change the intervals at any time.

    Can anyone point me at such a script?

    I have searched and searched and can not find something that works. I have found one that works but only works for one image, I CAN NOT get it to work for 3 different images being on the same page. This is the one that I use now but I don't know how to make it work with more then one image, and more then one path to images.


    <script language="JavaScript" type="text/javascript">
    <!--
    var Img1Ary=new Array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg');
    var Img1Path='http://MYSITE.com/files/SMLpics/';
    var ID1Ary=new Array('Img1');


    function Increment(c,ary){
    c++
    if (c>=ary.length){ c=0; }
    return c
    }

    function Rotate(idary,imgpath,imgary){
    if (!document.getElementById(idary[0]).set){
    document.getElementById(idary[0]).set=true;
    document.getElementById(idary[0]).cnt=-1;
    }
    for (zxc0=0;zxc0<idary.length;zxc0++){
    document.getElementById(idary[0]).cnt=Increment(document.getElementById(idary[0]).cnt,imgary);
    document.getElementById(idary[zxc0]).src=imgpath+imgary[document.getElementById(idary[0]).cnt];
    }
    }

    function Init(){
    setInterval('Rotate(ID1Ary,Img1Path,Img1Ary)',3000);
    }
    //-->
    </script>

    AND THEN IN HTML BODY I ADD: <body onload="Init();" ><img id="Img1" src="http://MYSITE.com/files/SMLpics/1.jpg"></body>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,678
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <img id="i1" src="http://www.vicsjavascripts.org.uk/StdImages/1.gif" />
    <img id="i2" src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" />
    <img id="i3" src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" />
    
    <script type="text/javascript">
    
    function RotateImage(id,ary,ms){
     var o=RotateImage[id],obj=document.getElementById(id),ok;
     if (!o&&obj&&obj.src){
      var src=obj.src.slice(0,obj.src.lastIndexOf('/')+1);
      o=RotateImage[id]={
       ary:[],
       ms:typeof(ms)=='number'&&ms>500?ms:1000,
       cnt:0
      }
      for (var z0=0;z0<ary.length;z0++){
       o.ary[z0]=new Image();
       o.ary[z0].src=src+ary[z0];
      }
      o.to=setTimeout(function(){ RotateImage(id); },o.ms);
     }
     else if (o){
      clearTimeout(o.to);
      o.cnt=++o.cnt%o.ary.length;
      ok=o.ary[o.cnt].width>40;
      ok?obj.src=o.ary[o.cnt].src:null;
      o.to=setTimeout(function(){ RotateImage(id); },ok?o.ms:100);
     }
    }
    
    RotateImage('i1',['1.gif','2.gif','3.gif','4.gif'],1000); // the image path is obtained  from the original image
    RotateImage('i2',['Egypt5.jpg','Egypt6.jpg','Egypt7.jpg','Egypt8.jpg'],3000);
    RotateImage('i3',['Egypt5.jpg','Egypt6.jpg','Egypt7.jpg','Egypt8.jpg'],2000);
    
    </script>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Feb 2012
    Posts
    23
    Hey Vic thanks for your help, I really need to figure this out. I tried the code and the first image shows up but it is not rotating the next images. What could be the problem? I have like 40 images in easy folder, does it matter that I added more under images to rotated under i1 i2 i3?

  4. #4
    Join Date
    Feb 2012
    Posts
    23
    <script type="text/javascript">

    function RotateImage(id,ary,ms){
    var o=RotateImage[id],obj=document.getElementById(id),ok;
    if (!o&&obj&&obj.src){
    var src=obj.src.slice(0,obj.src.lastIndexOf('/')+1);
    o=RotateImage[id]={
    ary:[],
    ms:typeof(ms)=='number'&&ms>500?ms:1000,
    cnt:0
    }
    for (var z0=0;z0<ary.length;z0++){
    o.ary[z0]=new Image();
    o.ary[z0].src=src+ary[z0];
    }
    o.to=setTimeout(function(){ RotateImage(id); },o.ms);
    }
    else if (o){
    clearTimeout(o.to);
    o.cnt=++o.cnt%o.ary.length;
    ok=o.ary[o.cnt].width>40;
    ok?obj.src=o.ary[o.cnt].src:null;
    o.to=setTimeout(function(){ RotateImage(id); },ok?o.ms:100);
    }
    }

    RotateImage('i1',['1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.j pg','1.jpg','1.jpg','3.jpg','5.jpg','7.jpg','9.jpg','11.jpg','13.jpg','15.jpg','17.jpg','19.jpg','21 .jpg','23.jpg','25.jpg','27.jpg','29.jpg','31.jpg','33.jpg','33.jpg','33.jpg','33.jpg','33.jpg','33. jpg','33.jpg','33.jpg','33.jpg','33.jpg','35.jpg','37.jpg','39.jpg','41.jpg','43.jpg','45.jpg','47.j pg','49.jpg','51.jpg','53.jpg','55.jpg','57.jpg','59.jpg','61.jpg','63.jpg','65.jpg'],40);
    RotateImage('i2',['1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.j pg','1.jpg','1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg','9.jpg','10.jpg','11.jpg ','11.jpg','11.jpg','11.jpg','11.jpg','11.jpg','11.jpg','11.jpg','11.jpg','11.jpg','12.jpg','13.jpg' ,'14.jpg','15.jpg','16.jpg','17.jpg','18.jpg','19.jpg','20.jpg'],40);
    RotateImage('i3',['1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.jpg','1.j pg','1.jpg','1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg','9.jpg','10.jpg','11.jpg ','12.jpg','13.jpg','14.jpg','15.jpg','16.jpg','17.jpg','18.jpg','19.jpg','20.jpg','20.jpg','20.jpg' ,'20.jpg','20.jpg','20.jpg','20.jpg','20.jpg','20.jpg','20.jpg','21.jpg','21.jpg','21.jpg','21.jpg', '21.jpg','21.jpg','21.jpg','21.jpg','21.jpg','21.jpg','22.jpg','23.jpg','24.jpg','25.jpg','26.jpg',' 27.jpg','28.jpg','29.jpg','30.jpg','31.jpg','32.jpg','33.jpg','34.jpg','35.jpg','36.jpg','37.jpg','3 8.jpg'],40);

    </script>

  5. #5
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,678
    what is the file path of the original image?
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  6. #6
    Join Date
    Feb 2012
    Posts
    23
    http://www.MYSITE.com/files/(1 of 3 diff folders)/1.jpg, 2.jpg, 3.jpg.....

    So I used:

    <img id="i1" src="http://www.MYSITE.com/files/repsol/1.jpg" />
    <img id="i2" src="http://www.MYSITE.com/files/rx7/1.jpg" />
    <img id="i3" src="http://www.MYSITE.com/files/audi/1.jpg" />

  7. #7
    Join Date
    Feb 2012
    Posts
    23
    Hey Vic thank you so much for the code, I was able to make it work. It was my html editing software that was messing stuff us, the code is actually A+++. Thanks again Sam

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles