www.webdeveloper.com
Results 1 to 5 of 5

Thread: Adding an auto scroll

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Adding an auto scroll

    Can anyone help me add and automatic scrolling effect to the following javascript. This code is on an older site using jquery 1.4.3. I was hoping it was just something I could simply add to this existing js so I don't have to redo the entire slideshow. Thanks!

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    // Save the jQuery objects for later use.
    var outer	= $("#preview_outer");
    var arrow	= $("#arrow");
    var thumbs	= $("#thumbs span");
    
    var preview_pos;
    var preview_els	= $("#preview_inner div");
    var image_width	= preview_els.eq(0).width(); // Get width of imaages
    
    // Hook up the click event
    thumbs.click(function() {
    // Get position of current image
    preview_pos = preview_els.eq( thumbs.index( this) ).position();
    
    // Animate them!
    outer.stop().animate( {'scrollLeft' : preview_pos.left},	500 );
    arrow.stop().animate( {'left' : $(this).position().left },	500 );
    });
    
    // Reset positions on load
    arrow.css( {'left' : thumbs.eq(0).position().left } ).show();
    outer.animate( {'scrollLeft' : 0}, 0 );
    
    // Set initial width
    $("#preview_inner").css('width', preview_els.length * image_width);
    });
    </script>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,680
    please provide a link to the page with the script
    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
    Jun 2013
    Posts
    3

    Sorry, guess that would have helped

    Quote Originally Posted by vwphillips View Post
    please provide a link to the page with the script
    http://millerbevco.com/

  4. #4
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,680
    using your current HTML and CSS

    remover your current script

    and position this immediately before the </BODY> tag

    Code:
    // Slider (12-June-2013)
    // by Vic Phillips - http://www.vicsjavascripts.org.uk/
    
    
    var zxcSlider={
    
     Next:function(id,ud){
      var o=this['zxc'+id],ud=(typeof(ud)=='number'&&ud<0?-1:1);
      if (o){
       o.ud=ud;
       this.rotate(o,o.cnt+ud);
      }
     },
    
     GoTo:function(id,nu){
      var o=this['zxc'+id];
      if (o&&o.ary[nu]){
       o.ud=nu>o.cnt?1:-1;
       this.rotate(o,nu)
      }
     },
    
     Auto:function(id,ms){
      var o=this['zxc'+id],oop=this;
      if (o){
       o.to=setTimeout(function(){ oop.rotate(o,true); },ms||200);
      }
     },
    
     Pause:function(id){
      var o=this['zxc'+id];
      if (o){
       clearTimeout(o.to);
      }
     },
    
     init:function(o){
      var id=o.SliderID,ms=o.Animate,ud=o.Direction,hold=o.Hold,srt=o.AutoStart,add=o.AddEvents,cls=o.ThumbsClass,p=document.getElementById(id),sd=p?p.getElementsByTagName('DIV')[0]:null,tp=document.getElementById(o.ThumbsID);;
      if (p&&sd){
       var clds=sd.childNodes,ary=[],thumbs=[],e,z0=0,z1=0,z2=0;
       for (;z0<clds.length;z0++){
        if (clds[z0].nodeType==1){
         clds[z0].style.position='absolute';
         clds[z0].style.zIndex=ary.length>0?'0':'2';
         clds[z0].style.left=(ary.length>0?3000:0)+'px';
         clds[z0].style.top='0px';
         ary.push([clds[z0],'left']);
        }
       }
       if (tp){
        clds=tp.childNodes;
        for (;z1<clds.length;z1++){
         if (clds[z1].nodeType==1){
          this.addevt(clds[z1],'mouseup','GoTo',id,thumbs.length);
          this.addevt(clds[z1],'mouseover','mse',id,clds[z1]);
          this.addevt(clds[z1],'mouseout','mse',id,clds[z1]);
          thumbs.push(clds[z1]);
         }
        }
       }
       this['zxc'+id]={
        id:id,
        ary:ary,
        lgth:ary.length-1,
        thumbs:thumbs,
        cls:cls,
        ct:thumbs[0],
        ms:ms,
        hold:typeof(hold)=='number'&&hold>0?hold:ms*4,
        cnt:0,
        lst:0,
        ud:typeof(ud)=='number'&&ud<0?-1:1
       }
       if (add instanceof Array){
        for (;z2<add.length;z2++){
         if (add[z2]&&add[z2][0]){
          o=document.getElementById(add[z2][0]);
          e=add[z2][1];
          if (o&&(e=='mouseup'||e=='mousedown'||e=='mouseover'||e=='mouseout'||e=='click')&&this[add[z2][2]]){
           this.addevt(o,e,add[z2][2],id,add[z2][3]);
          }
         }
        }
       }
       typeof(srt)=='number'&&srt>0?this.Auto(id,srt):null;
      }
     },
    
     rotate:function(o,auto){
      this.Pause(o.id);
      var c=o.cnt,d=new Date(),sz;
      o.ary[o.lst][0].style.zIndex='0';
      o.ary[c][0].style.zIndex='1';
      o.lst=c;
      o.auto=auto===true;
      c=auto===true?c+=o.ud:auto;
      c=c<0?o.lgth:c>o.lgth?0:c;
      o.ary[c][0].style.zIndex='2';
      sz=o.ary[o.ud>0?o.cnt:c][0].offsetWidth;
      this.animate(o,o.ary[c],sz*(o.ud>0?1:-1),0,d,o.ms);
      this.animate(o,o.ary[o.cnt],0,sz*(o.ud>0?-1:1),d,o.ms,true);
      o.cls&&o.ct?o.ct.className='':null;
      o.ct=o.cls&&o.thumbs[c]?o.thumbs[c]:null;
      o.cls&&o.ct?o.ct.className=o.cls:null;
      o.cnt=c;
     },
    
     animate:function(o,a,f,t,srt,mS,ud){
      clearTimeout(a[4]);
      var oop=this,ms=new Date().getTime()-srt,n=(t-f)/mS*ms+f;
      if (isFinite(n)){
       a[0].style[a[1]]=n+'px';
       a[2]=n;
      }
      if (ms<mS){
       a[4]=setTimeout(function(){ oop.animate(o,a,f,t,srt,mS,ud); },10);
      }
      else {
       a[0].style[a[1]]=(ud?3000:t)+'px';
       o.auto&&ud?oop.Auto(o.id,o.hold):null;
      }
     },
    
     mse:function(id,obj,e){
      var o=this['zxc'+id];
      if (o&&obj!=o.ct&&o.cls){
       obj.className=e.type=='mouseout'?'':'onClick';
      }
     },
    
     addevt:function(o,t,f,p,p1){
      var oop=this;
      if (o.addEventListener){
       o.addEventListener(t,function(e){ return oop[f](p,p1,e);}, false);
      }
      else if (o.attachEvent){
       o.attachEvent('on'+t,function(e){ return oop[f](p,p1,e); });
      }
     }
    
    
    
    }
    
    
    zxcSlider.init({
     SliderID:'preview_outer', // the unique ID name of the slider parent DIV.                           (string)
     ThumbsID:'thumbs',        // the unique ID name of the thumbnals parent DIV.                        (string)
     ThumbsClass:'onClick',    //(optional) the class name of the active and mouseover thumbnails.       (string, default = no class name change)
     Animate:1000,             //(optional) the animation duration in milli seconds.                     (number, default = 1000)
     Hold:2000,                //(optional) the auto rotation 'hold' delay milli seconds.                (number, default = Animate*4)
     Direction:1,              //(optional) the auto rotate direction, > 0 = anticlock, < 0 = clockwise. (number, default = > 0 = clockwise)
     AutoStart:5000,           //(optional) the delay milli seconds before starting auto rotate.         (number, default = no auto start)
     AddEvents:[               //(optional) an array defining event calls to add.                        (array, default = in line event calls)
     // field 0 = the unique ID name of the element.
     // field 1 = the event type.
     // field 0 = the script function name.
     // field 0 = the parameter to pass to the function.
    
     ['buttonLeft','mouseup','Next',1],
     ['buttonRight','mouseup','Next',-1],
     ['preview_wrap','mouseover','Pause'],
     ['preview_wrap','mouseout','Auto']
     ]
    
    });
    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/

  5. #5
    Join Date
    Jun 2013
    Posts
    3
    Wow Vic, that is exactly what I needed. Thank you so much.

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