Bootstrap-4.3中的多图像滑动轮播问题

时间:2019-10-31 10:57:53

标签: css twitter-bootstrap bootstrap-4 carousel

我试图制作一个轮播,其中包含多个连续的图像,并且每次仅使用引导程序一次滑动一个图像。像这样:

enter image description here

我尝试遵循此post。我使用的是 bootstrap-3.3.6jquery-2.2.2 ,而不是 bootstrap-4.3.1jquery-3.3.1 ,但在我的情况下,图像位于垂直订单,而不是水平订单。我究竟做错了什么?如何按水平顺序排列图像?

$(document).ready(function () {
  $('.fdi-Carousel .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));
    }
    else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
  });
});
#outerCarousel {
  width: 100%;
}
.carousel-control-prev-icon,
.carousel-control-next-icon {
  background-color: tomato !important;
}
.carousel-inner.onebyone-carousel {
  margin: auto;
  width: 90%;
}
.onebyone-carousel .active.carousel-item-left {
  left: -33.33%;
}
.onebyone-carousel .active.carousel-item-right {
  left: 33.33%;
}
.onebyone-carousel .carousel-item-next {
  left: 33.33%;
}
.onebyone-carousel .carousel-item-prev {
  left: -33.33%;
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body>
    <main role="main">
      <div class="container">
        <div class="row">
          <div id="outerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="5000"
               data-pause="false">
            <div id="innerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="0"
                 data-pause="false">
              <div class="carousel-inner onebyone-carousel">
                <div class="carousel-item active">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/4287f5/000&amp;text=1">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/f57b42/000&amp;text=2">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/42f58a/000&amp;text=3">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/a442f5/000&amp;text=4">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/d1f542/000&amp;text=5">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/f5429e/000&amp;text=6">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-md-4">
                    <img src="http://placehold.it/100/42cef5/000&amp;text=7">
                  </div>
                </div>
              </div>
              <a class="carousel-control-prev" href="#innerCarousel" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
              </a>
              <a class="carousel-control-next" href="#innerCarousel" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
              </a>
            </div>
          </div>
        </div>
      </div>
    </main>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
            integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
            integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
            crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
            integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
            crossorigin="anonymous"></script>
  </body>
</html>

谢谢。

2 个答案:

答案 0 :(得分:2)

首先,您提供的代码有很多错误。我相信,有很多在线轮播示例只是从他人那里复制粘贴而从未尝试理解代码,相反,他们会不断添加更多的错误代码来解决他们可以“看到”的问题,请小心。

有许多无用的<div>包装程序和CSS样式旨在修复错误的行为,这显然会使调试过程变得更糟。

我建议您可以先查看官方文档,以了解这些组件的结构,然后根据SO的答案慢慢实现想法。

我只是从您的代码中删除其中大部分,仅用于演示目的。

为了使其具有响应性,我相信您必须修改js代码,以便它可以为不同的col-size和CSS填充轮播。但是似乎没有一种简单的方法可以解决CSS部分。

$(document).ready(function () {
  $('.fdi-Carousel .carousel-item').each(function () {
    var next = $(this).next();
    if (!next.length) {
      next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));
    
    //this will only work for carousel with 3 items since it only adds next() and  next.next();

    if (next.next().length > 0) {
      next.next().children(':first-child').clone().appendTo($(this));
    }
    else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
  });
});
.carousel-control-prev-icon,
.carousel-control-next-icon {
  background-color: tomato !important;
}

.fdi-Carousel .carousel-inner .active.carousel-item,
.fdi-Carousel .carousel-inner .carousel-item-next,
.fdi-Carousel .carousel-inner .carousel-item-prev {
  display: flex;
} 

/* 'Again 33% means col-4, in order to make it responsive or whatever you want, you have to create different translateX()  for differernt col' */
.fdi-Carousel .carousel-inner .active.carousel-item-left, .fdi-Carousel .carousel-inner .carousel-item-prev {
  transform: translateX(-33.33%);
}
.fdi-Carousel .carousel-inner .active.carousel-item-right, .fdi-Carousel .carousel-inner .carousel-item-next {
  transform: translateX(33.33%);
}

.fdi-Carousel .carousel-inner .carousel-item-left, .fdi-Carousel .carousel-inner .carousel-item-right {
  transform: translateX(0);
  }
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
     <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"  crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </head>
  <body>
      <div class="container-fluid">
        <div class="row w-100">
          <div class="col">
          <div id="outerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="5000">
              <div class="carousel-inner row no-gutters">
                <div class="carousel-item active">
                  <div class="col-4 text-center">
                    <img src="http://placehold.it/100/4287f5/000&amp;text=1">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-4 text-center">
                    <img src="http://placehold.it/100/f57b42/000&amp;text=2">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-4 text-center">
                    <img src="http://placehold.it/100/42f58a/000&amp;text=3">
                  </div>
                </div>
                <div class="carousel-item">
                  <div class="col-4 text-center">
                    <img src="http://placehold.it/100/a442f5/000&amp;text=4">
                  </div>
                </div>
              </div>
              <a class="carousel-control-prev" href="#outerCarousel" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
              </a>
              <a class="carousel-control-next" href="#outerCarousel" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
              </a>
          </div>
        </div>
      </div>
      </div>
  </body>
</html>

答案 1 :(得分:0)

$(document).ready(function() {
  $('.fdi-Carousel .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));
    } else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
  });
});
#innerCarousel {
  width: 100%;
}

.carousel-control-prev-icon,
.carousel-control-next-icon {
  background-color: tomato !important;
}

.carousel-inner.onebyone-carousel {
  margin: auto;
  width: 90%;
}

.onebyone-carousel .active.carousel-item-left {
  left: -33.33%;
}

.onebyone-carousel .active.carousel-item-right {
  left: 33.33%;
}

.onebyone-carousel .carousel-item-next {
  left: 33.33%;
}

.onebyone-carousel .carousel-item-prev {
  left: -33.33%;
}

.carousel-item img {
  width: 33%;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
  <main role="main">
    <div class="container">
      <div class="row">
        <div id="innerCarousel" class="carousel fdi-Carousel slide" data-ride="carousel" data-interval="5000" data-pause="false">
          <div class="carousel-inner onebyone-carousel">
            <div class="carousel-item active">
              <img src="http://placehold.it/100/4287f5/000&amp;text=1">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/f57b42/000&amp;text=2">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/42f58a/000&amp;text=3">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/a442f5/000&amp;text=4">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/d1f542/000&amp;text=5">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/f5429e/000&amp;text=6">
            </div>
            <div class="carousel-item">
              <img src="http://placehold.it/100/42cef5/000&amp;text=7">
            </div>
          </div>
          <!-- .carousal-inner -->

          <a class="carousel-control-prev" href="#innerCarousel" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="carousel-control-next" href="#innerCarousel" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>
        <!-- .carousel -->
      </div>
      <!-- .row -->
    </div>
    <!-- .container -->
  </main>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

使用一些HTML和CSS修改来固定代码。该修复程序将使轮播图像以水平布局显示。这是我更改的内容列表

  1. 在轮播中为img添加了宽度
  2. 删除了div.col-md-4
  3. 已删除#outerCarousel