【问题标题】:Issue while creating image slider using jquery使用 jquery 创建图像滑块时出现问题
【发布时间】:2013-11-14 15:42:24
【问题描述】:

我使用 jquery 创建了一个图像滑块。我得到了完美的输出,但我的问题是 它运行单个循环,即如果有 4 个图像,那么在最后一个图像之后它停止我想让它循环,即在最后一个图像滑块应该显示第一个图像之后。

我的代码是:

<style type="text/css">
    .active{
        z-index:99;
    }
</style>

<html>
    <head>
    </head>
    <body>
        <div id="slideshow">
            <img src="1.jpg" style="position:absolute;" class="active" />
            <img src="2.jpg" style="position:absolute;" />
            <img src="3.jpg" style="position:absolute;" />
        </div>
    </body>
    <script src="jquery-1.10.1.min.js"></script>
    <script>

    function slideSwitch() {
        var $active = $('div#slideshow IMG.active');
        var $next = $active.next();    

        $next.addClass('active');      

        $active.removeClass('active');      


    }

    $(function() {
        setInterval( "slideSwitch()", 2000 );
        /*if($('#slideshow:last-child').hasClass('active'))
        {
            alert("Complete");
        }*/
    });
</script>
</html>

如何在循环模式下运行滑块。

应用这个简单滑块的原因是我想进一步自定义滑块。

【问题讨论】:

    标签: javascript jquery slider


    【解决方案1】:

    试试这个代码:-

              function slideSwitch() {
                    var $active = $('div#slideshow img.active'),
                    $next;    
    
                    if($('div#slideshow img.active').is(':last-child'))
                    {
                        $next = $('div#slideshow img').first();  // get the first image 
    
                    }else{
                        $next = $active.next();    
                    }
    
                    $next.addClass('active');      
    
                    $active.removeClass('active'); 
                }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      function slideSwitch() {
              var $active = $('div#slideshow IMG.active');
              var $next = $active.next();    
              if($next.length == 0) $next = $('div#slideshow IMG:first');
              $next.addClass('active');
              $active.removeClass('active');      
      }
      

      【讨论】:

        【解决方案3】:

        给你:

        JS 小提琴:http://jsfiddle.net/3wAxB/

        function slideSwitch() {
            var $active = $('div#slideshow IMG.active');
            var $next = $active.next();
            if($next.length==0){
                $next=$('#slideshow img').first();
            }
        
            $next.addClass('active');      
        
            $active.removeClass('active');      
        
        
        }
        $(function() {
            setInterval(slideSwitch, 2000 );
        });
        

        另外看看我对这个问题的回答,它可能对你有用:Very short jQuery Image Slideshow

        【讨论】:

          猜你喜欢
          • 2014-11-01
          • 2013-07-30
          • 2016-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多