javascript - 当元素少于 3 时,旋转木马 slider 停止克隆

标签 javascript jquery html css twitter-bootstrap

我正在使用 Bootstrap 轮播 slider ,现在我的 slider 一直在重复。当元素计数少于 3 时,我需要停止重复元素。我尝试了以下脚本。任何人都可以帮助我实现这一目标。

    <div id="carousel" class="carousel slide" data-ride="carousel" data-interval="false">
    <div class="carousel-inner">
        <div class="item active">
            <img src="images/homogeneous/marvel-stone/marvel-stone-detail.jpg" alt="">
        </div>
    </div>
</div>
<div class="clearfix">
    <div id="myCarousel" class="carousel slide" data-interval="false">
        <div class="carousel-inner">
            <div class="item active">                
                <div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb1.png" alt=""></div>
            </div>
            <div class="item">
                <div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb2.png" alt=""></div>
            </div>
            <div class="item">
                <div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb3.png" alt=""></div>
            </div>
            <div class="item">
                <div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb4.png" alt=""></div>
            </div>
        </div>
        <!-- /carousel-inner -->
        <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
       <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
        <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
       <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
    </div>
    <!-- /myCarousel -->
</div>
<!-- /clearfix -->

我尝试了 3 个元素的脚本。但是我不知道如何用简化的脚本编写 2 项和 1 项。

 $('#myCarousel').carousel({
     interval: false
 });
 var totalItems = $('.item').length;
 if (totalItems > 3) {
     //alert();
     $('.carousel .item').each(function() {
         var next = $(this).next();
         if (!next.length) {
             next = $(this).siblings(':first');
         }
         next.children(':first-child').clone().appendTo($(this));

         if (next.next().length > 0) {

             next.next().children(':first-child').clone().appendTo($(this)).addClass('rightest');

         } else {
             $(this).siblings(':first').children(':first-child').clone().appendTo($(this));

         }
     });
 } else {

     //what to be here
 }

my code here

最佳答案

这个为我工作。

$('#myCarousel').carousel({ 
              interval: false                 
            });
            var totalItems = $('.item').length;
        if (totalItems > 3) {
            //alert();
            $('.carousel .item').each(function(){
              var next = $(this).next();
              if (!next.length) {
                next = $(this).siblings(':first');
              }
              next.children(':first-child').clone().appendTo($(this));

              if (next.next().length>0) {

                  next.next().children(':first-child').clone().appendTo($(this)).addClass('rightest');

              }
              else {
                  $(this).siblings(':first').children(':first-child').clone().appendTo($(this));

              }
            }); 
        }
        else {
            //alert("hello");
            (function(){
          $('.carousel .item').each(function(){
            var itemToClone = $(this);

            for (var i=1;i<2;i++) {
              itemToClone = itemToClone.next();     
              // wrap around if at end of item collection
              if (!itemToClone.length) {
                itemToClone = $(this).siblings(':first');
              }             
              // grab item, clone, add marker class, add to collection
              itemToClone.children(':first-child').clone()
                .addClass("cloneditem-"+(i))
                .appendTo($(this)); 

              //listener for after slide
                jQuery('.carousel').on('slid.bs.carousel', function(){

              //Each slide has a .item class to it, you can get the total number of slides like this
                    var totalItems = jQuery('.carousel .item').length;

              //find current slide number
                    var currentIndex = jQuery('.carousel .item div.active').index() + 1;

              //if slide number is last then stop carousel
                  if(totalItems == currentIndex){

                    clearInterval(jQuery('.carousel .item').data('bs.carousel').interval);

                  } // end of if

           });

            }

          });
        }()); 
            }

关于javascript - 当元素少于 3 时,旋转木马 slider 停止克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493309/

相关文章:

javascript - 当索引是闭包的一部分时,如何创建索引?

php - 更高目录中的嵌入式页面不使用样式表

javascript - 加载后在 url 中使用 anchor 标记

html - 使用 CSS 文本上的图像边框?

html - 我的导航栏中的边框线在它们的右侧有一点边距

javascript - 从本地 js 脚本对本地 Phantom.js 服务器的 Ajax 调用

javascript - 以正确的方式提取 HTML

javascript - 在 HTML 中重复 A4 尺寸样式

javascript - 为什么我不能在任何地方为 jQuery 使用 &lt;script&gt; 标签?

javascript - 粘性侧边栏和固定列