www.webdeveloper.com
Results 1 to 7 of 7

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

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    24

    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,709
    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
    24
    <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>

  4. #4
    Join Date
    Feb 2012
    Posts
    24
    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?

  5. #5
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,709
    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
    24
    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

  7. #7
    Join Date
    Feb 2012
    Posts
    24
    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" />

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.26791 seconds
  • Memory Usage 3,097KB
  • Queries Executed 13 (?)
More Information
Template Usage (30):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates