简体   繁体   English

有人可以解释如何使此jQuery滑块自动播放和循环吗?

[英]Can someone explain how to make this jQuery slider autoplay and loop?

Here's the slider that I'm trying to loop: 这是我要循环播放的滑块:

http://sixrevisions.com/demo/slideshow/final.html 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. 到jQuery编码的末尾,并每5秒钟将其移至下一张幻灯片。 However, after the last slide, it just continues to "click" the rightControl button. 但是,在最后一张幻灯片之后,它只是继续“单击” rightControl按钮。 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() }
  } 
});

I think this jsfiddle is what you're looking for. 我认为这个jsfiddle是您想要的。

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. 我继续并添加了左移支票,但您可能不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM