web-dev-qa-db-ja.com

Bootstrap 4複数アイテムカルーセル(クリックするとすべての新しいアイテムが循環します)

これを行うためにbootstrap 4カルーセル機能を取得しようとしています(下の画像は bootstrap 3バリアント

enter image description here

これをbootstrap 4.に変換してみました。これを実行したのは bootstrap3→to bootstrap 4 html変換ツール htmlで、CDNからのbootstrap JSおよびCSSファイルへの参照を調整します。また、表示する画像を簡単に区別できるように、画像参照を色で変更しました。

以下はコードです。1つのスライドに4つの画像すべてが表示されているわけではありません

$(document).ready(function() {
    $('#Carousel').carousel({
        interval: 5000
    })
});
body{padding-top:20px;}
.carousel {
    margin-bottom: 0;
    padding: 0 40px 30px 40px;
}
/* The controlsy */
.carousel-control {
        left: -12px;
    height: 40px;
        width: 40px;
    background: none repeat scroll 0 0 #222222;
    border: 4px solid #FFFFFF;
    border-radius: 23px 23px 23px 23px;
    margin-top: 90px;
}
.carousel-control.right {
        right: -12px;
}
/* The indicators */
.carousel-indicators {
        right: 50%;
        top: auto;
        bottom: -10px;
        margin-right: -19px;
}
/* The colour of the indicators */
.carousel-indicators li {
        background: #cecece;
}
.carousel-indicators .active {
background: #428bca;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!------ Include the above in your HEAD tag ---------->

<div class="container">
  <div class="row">
    <div class="col-lg-12">
      <div id="Carousel" class="carousel slide">
        <ol class="carousel-indicators">
          <li data-target="#Carousel" data-slide-to="0" class="active"></li>
          <li data-target="#Carousel" data-slide-to="1"></li>
          <li data-target="#Carousel" data-slide-to="2"></li>
        </ol>
        <!-- Carousel items -->
        <div class="carousel-inner">
          <div class="item active carousel-item">
            <div class="row">
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" alt="Image" style="max-width:100%;"></a>
              </div>
            </div>
            <!--.row-->
          </div>
          <!--.item-->
          <div class="item carousel-item">
            <div class="row">
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" alt="Image" style="max-width:100%;"></a>
              </div>
            </div>
            <!--.row-->
          </div>
          <!--.item-->
          <div class="item carousel-item">
            <div class="row">
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a>
              </div>
              <div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></a>
              </div>
            </div>
            <!--.row-->
          </div>
          <!--.item-->
        </div>
        <!--.carousel-inner--><a data-slide="prev" href="#Carousel" class="left carousel-control">&#x2039;</a>
        <a data-slide="next" href="#Carousel" class="right carousel-control">&#x203A;</a>
      </div>
      <!--.Carousel-->
    </div>
  </div>
</div>
<!--.container-->
1
Vincent Tang

これを確認してくださいbootstrap v4カルーセルと4つのアイテム:

.col-md-3{
  display: inline-block;
  margin-left:-4px;
}
.col-md-3 img{
  width:100%;
  height:auto;
}
body .carousel-indicators li{
  background-color:red;
}
body .carousel-indicators{
  bottom: 0;
}
body .carousel-control-prev-icon,
body .carousel-control-next-icon{
  background-color:red;
}
body .no-padding{
  padding-left: 0;
  padding-right: 0;
   }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>      
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>
  
  <!-- The slideshow -->
  <div class="container carousel-inner no-padding">
    <div class="carousel-item active">
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>    
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
    </div>
    <div class="carousel-item">
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>    
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>  
    </div>
    <div class="carousel-item">
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>    
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>   
      <div class="col-xs-3 col-sm-3 col-md-3">
        <img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
      </div>  
    </div>
  </div>
  
  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

このペンも表示できます https://codepen.io/kravisingh/pen/pLGzgo

5
kravisingh

これはあなたが望んでいたものですか?

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=Edge">
  <title>Document</title>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <style>
    .carousel-inner .active.left {
      left: -33%;
    }

    .carousel-inner .next {
      left: 33%;
    }

    .carousel-inner .prev {
      left: -33%;
    }

    .carousel-control.left,
    .carousel-control.right {
      background-image: none;
    }

    .carousel-multi .carousel-inner>.item {
      transition: 500ms ease-in-out left;
    }

    .carousel-multi .carousel-inner>.item>.media-card {
      background: #333;
      border-right: 10px solid #fff;
      display: table-cell;
      width: 1%;
    }

    .carousel-multi .carousel-inner>.item>.media-card:last-of-type {
      border-right: none;
    }

    .carousel-multi .carousel-inner .active {
      display: table;
    }

    .carousel-multi .carousel-inner .active.left {
      left: -33%;
    }

    .carousel-multi .carousel-inner .active.right {
      left: 33%;
    }

    .carousel-multi .carousel-inner .next {
      left: 33%;
    }

    .carousel-multi .carousel-inner .prev {
      left: -33%;
    }

    @media all and (transform-3d),
    (-webkit-transform-3d) {
      .carousel-multi .carousel-inner>.item {
        transition: 500ms ease-in-out all;
        backface-visibility: visible;
        transform: none!important;
      }
    }
  </style>
</head>

<body>



  <div id="carousel-example-multi" class="carousel carousel-multi slide">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#carousel-example-multi" data-slide-to="0" class="active"></li>
      <li data-target="#carousel-example-multi" data-slide-to="1"></li>
      <li data-target="#carousel-example-multi" data-slide-to="2"></li>
      <li data-target="#carousel-example-multi" data-slide-to="3"></li>
      <li data-target="#carousel-example-multi" data-slide-to="4"></li>
      <li data-target="#carousel-example-multi" data-slide-to="5"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <div class="media media-card">
          <time class="small">10 Aug 2016</time>
          <div class="media-category">Commodities Quarterly</div>
          <h4 class="media-heading">
            <a href="#">Research article 1</a>
          </h4>
          <a class="aside" href="#">Read more</a>
        </div>
      </div>

      <div class="item ">
        <div class="media media-card">
          <time class="small">10 Aug 2016</time>
          <div class="media-category">Commodities Quarterly</div>
          <h4 class="media-heading">
            <a href="#">Research article number two</a>
          </h4>
          <a class="aside" href="#">Read more</a>
        </div>
      </div>

      <div class="item ">
        <div class="media media-card">
          <time class="small">10 Aug 2016</time>
          <div class="media-category">Commodities Quarterly</div>
          <h4 class="media-heading">
            <a href="#">Number 3</a>
          </h4>
          <a class="aside" href="#">Read more</a>
        </div>
      </div>

      <div class="item ">
        <div class="media media-card">
          <time class="small">10 Aug 2016</time>
          <div class="media-category">Commodities Quarterly</div>
          <h4 class="media-heading">
            <a href="#">This is the fourth article</a>
          </h4>
          <a class="aside" href="#">Read more</a>
        </div>
      </div>

      <div class="item ">
        <div class="media media-card">
          <time class="small">10 Aug 2016</time>
          <div class="media-category">Commodities Quarterly</div>
          <h4 class="media-heading">
            <a href="#">...and this is the fifth</a>
          </h4>
          <a class="aside" href="#">Read more</a>
        </div>
      </div>

      <div class="item ">
        <div class="media media-card">
          <time class="small">10 Aug 2016</time>
          <div class="media-category">Commodities Quarterly</div>
          <h4 class="media-heading">
            <a href="#">Sixth</a>
          </h4>
          <a class="aside" href="#">Read more</a>
        </div>
      </div>

    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example-multi" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-multi" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  <script>
    //Allows bootstrap carousels to display 3 items per page rather than just one
    $('.carousel.carousel-multi .item').each(function () {
      var next = $(this).next();
      if (!next.length) {
        next = $(this).siblings(':first');
      }
      next.children(':first-child').clone().attr("aria-hidden", "true").appendTo($(this));

      if (next.next().length > 0) {
        next.next().children(':first-child').clone().attr("aria-hidden", "true").appendTo($(this));
      }
      else {
        $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
      }
    });
  </script>
</body>

</html>

https://codepen.io/MarkitDigital/pen/ZpEByz

1
Cuong Vu