Help us understand the problem. What is going on with this article?

Bootstrap4でスライド表示(carousel)

More than 1 year has passed since last update.

概要

フロントフレームワークを使ってみたのでメモする。

サンプル

<!DOCTYPE html>
<html lang="ja">
<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>BootStrap Sample</title>

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
  <div class="container">
    <div class="row">
      <div class="col-12">

        <div class="carousel slide" id="carousel">
          <div class="carousel-inner">
            <div class="carousel-item active">
              <img src="../img/sample1.png" alt="サンプル1" class="d-block w-100">
            </div>
            <div class="carousel-item">
              <img src="../img/sample2.png" alt="サンプル2" class="d-block w-100">
            </div>
            <div class="carousel-item">
              <img src="../img/sample3.png" alt="サンプル3" class="d-block w-100">
            </div>
            <div class="carousel-item">
              <img src="../img/sample4.png" alt="サンプル4" class="d-block w-100">
            </div>
            <div class="carousel-item">
              <img src="../img/sample5.png" alt="サンプル5" class="d-block w-100">
            </div>
            <div class="carousel-item">
              <img src="../img/sample6.png" alt="サンプル5" class="d-block w-100">
            </div>
          </div>

          <!-- インジケータ -->
          <ul class="carousel-indicators">
            <li data-target="#carousel" data-slide-to="0" class="d-none d-sm-block active"></li>
            <li data-target="#carousel" data-slide-to="1" class="d-none d-sm-block"></li>
            <li data-target="#carousel" data-slide-to="2" class="d-none d-sm-block"></li>
            <li data-target="#carousel" data-slide-to="3" class="d-none d-sm-block"></li>
            <li data-target="#carousel" data-slide-to="4" class="d-none d-sm-block"></li>
            <li data-target="#carousel" data-slide-to="5" class="d-none d-sm-block"></li>
          </ul>

          <!-- prev/nextのボタン -->
          <a href="#carousel" class="carousel-control-prev" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          </a>
          <a href="#carousel" class="carousel-control-next" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
          </a>

        </div>

        <!-- スライド再開/停止/終了/前へ/次へのボタン -->
        <div class="text-center m-1">
          <div class="row">
            <div class="col-2"></div>
            <div class="col-2 p-1">
              <button type="button" class="btn btn-primary w-100" id="prev">前へ</button>
            </div>
            <div class="col-2 p-1">
              <button type="button" class="btn btn-primary w-100" id="start">開始</button>
            </div>
            <div class="col-2 p-1">
              <button type="button" class="btn btn-primary w-100" id="stop">停止</button>
            </div>
            <div class="col-2 p-1">
              <button type="button" class="btn btn-primary w-100" id="next">次へ</button>
            </div>
            <div class="col-2"></div>
          </div>
        </div>

      </div>
    </div>
  </div>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <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>
  <script>
    (function($){
      const slide = $('#carousel');
      slide
        .carousel({
          interval: 2000,
          pause   : false,
          ride    : false
        })
        .on('click', () => {
          // クリックしたら、スライドを停止する
          slide.carousel('pause');
        })
        .on('slide.bs.carousel', (e) => {
          // prev,nextなどのslideメソッドが実行されると実行される
          console.log('change slide from ' + e.from + ' to ' + e.to + '(' + e.direction + ')');
        })
        .on('slid.bs.carousel', (e) => {
          // スライド遷移が完了すると実行される
          const img = $('img', e.relatedTarget);
          console.log('show slide No.' + e.to + '(', img.attr('src') + ')');
        });

      // ボタン(スライド開始)
      $('#start').on('click', () => {
        slide.carousel('cycle');
      });

      // ボタン(スライド停止)
      $('#stop').on('click', () => {
        slide.carousel('pause');
        // slide.carousel('dispose');
      });

      // ボタン(前へ)
      $('#prev').on('click', () => {
        slide.carousel('prev');
      });

      // ボタン(次へ)
      $('#next').on('click', () => {
        slide.carousel('next');
      });

    })(jQuery);
  </script>
</body>
</html>

動かしてみる。
slide.png

コンソールを確認してみる。
console.png

以上

参考サイト

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away