有人能解释一下如何使这个jQuery滑块自动播放和循环



这是我要循环的滑动条:

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

我可以添加

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

到jQuery编码的末尾,并使其每5秒移动到下一张幻灯片。然而,在最后一张幻灯片之后,它只是继续"点击"右控制按钮。如何让它在最后一张幻灯片之后循环播放?

<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() }
  } 
});

我想这就是你要找的。

基本上代替这个:

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

与下面的,检查是否在幻灯片的一端:

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

我还添加了向左移动的检查,但你可能不需要它

相关内容

  • 没有找到相关文章

最新更新