0

Here's the slider that I'm trying to loop:

http://sixrevisions.com/demo/slideshow/final.html

I was able to add

timer = window.setInterval("autoSlide()", 5000); 
function autoSlide(){ jQuery("#rightControl").click(); }

to the end of the jQuery coding and get it to move to the next slide every 5 seconds. However, after the last slide, it just continues to "click" the rightControl button. How do I get it to loop after the last slide?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 560;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
    currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

// Hide / show controls
manageControls(currentPosition);
// Move slideInner using margin-left
$('#slideInner').animate({
  'marginLeft' : slideWidth*(-currentPosition)
});
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  } 
});
  • If you want it to loop, then why are you hiding the controls: `if(position==0){ $('#leftControl').hide()`? Also, I would suggest [using semicolons](http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript). – ShadowCat7 Mar 24 '14 at 19:48

1 Answers1

0

I think this jsfiddle is what you're looking for.

Basically replace this:

currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

With the below, which checks for being at one end of the slideshow:

if ($(this).attr('id') == 'rightControl') { currentPosition = currentPosition >= slides.length - 1 ? 0 : currentPosition + 1; } else { currentPosition = currentPosition <= 0 ? slides.length - 1 : currentPosition - 1; }

I went ahead and added the check for moving left as well, but you probably won't need it.

ShadowCat7
  • 804
  • 8
  • 16