【问题标题】:Simple slideshow jQuery, need it to end after 5 slides. Help?简单的幻灯片 jQuery,需要在 5 张幻灯片后结束。帮助?
【发布时间】:2011-11-07 23:03:35
【问题描述】:

我有一点 javascript/jQuery 可以创建一个简单的幻灯片。我去这里时正在学习 jquery ......而且我很难过如何修改这部分 JS 以在停止之前循环 x 次。截至目前,它在无限循环中进行幻灯片放映。有什么建议么?谢谢!

<script>
function slideSwitch() {
    var $active = $('#slideshow DIV.active');

if ( $active.length == 0 ) $active = $('#slideshow DIV.item:last');

var $next =  $active.next().length ? $active.next()
    : $('#slideshow DIV.item:first');

$active.addClass('last-active');

$next.css({opacity: 0.0})
    .addClass('active')
    .animate({opacity: 1.0}, 1000, function() {
        $active.removeClass('active last-active');
        });
}

    $(function() {
        setInterval( "slideSwitch()", 5000 );
    });
</script>

【问题讨论】:

    标签: jquery slideshow


    【解决方案1】:

    5 次后只需clearTimeout():

    var timeout, count = 0, x = 5; // change 5 to the amount of times you want it to go
    
    function slideSwitch()
    {
    
      var $active = $('#slideshow DIV.active');
    
      if ( $active.length == 0 ) $active = $('#slideshow DIV.item:last');
    
      var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV.item:first');
    
      $active.addClass('last-active');
    
      $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
          $active.removeClass('active last-active');
        });
    
      count++;
      if(count == x) clearTimeout(timeout);
    
    }
    
    $(function() {
        timeout = setInterval(slideSwitch, 5000);
    });
    

    【讨论】:

      【解决方案2】:
      <script>
      
      var loop = 0;
      
      function slideSwitch() {
      
          var $active = $('#slideshow DIV.active');
      
      if ( $active.length == 0 ) $active = $('#slideshow DIV.item:last');
      
      var $next =  $active.next().length ? $active.next()
          : $('#slideshow DIV.item:first');
      
      $active.addClass('last-active');
      
      $next.css({opacity: 0.0})
          .addClass('active')
          .animate({opacity: 1.0}, 1000, function() {
              $active.removeClass('active last-active');
              });
      }
          loop++;
      
      
          $(function() {
              if(loop < 5){
                 setInterval( "slideSwitch()", 5000 );
              }
          });
      </script>
      

      【讨论】:

      • 我试过这个版本,但它仍然处于无限循环中。我得到了其他人发布的工作版本..但感谢您的时间:)
      猜你喜欢
      • 1970-01-01
      • 2014-11-22
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2015-04-21
      • 1970-01-01
      相关资源
      最近更新 更多