Bootstrapで画像スライダーを簡単に作る【Carousel】

簡単な備忘録です。

Bootstrapを使ったサイトを制作しており、スライダーの表示方法を調べていると、超簡単な方法があったので、ここにメモしておきます。

*Bootstrapの準備段階の手順は省きます。

こんな感じのスライダーが一瞬でできます。
デモ


Bootstrapのスライダー:HTMLの準備

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<meta name="X-UA-Compatible" content="IE=Edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Bootstrap-slider</title>

	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>

	<div class="worksslider inside">
		<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="3000">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="画像URL" alt="">
      <div class="carousel-caption">
        キャプション
      </div>
    </div>
    <div class="item">
      <img src="画像URL" alt="">
      <div class="carousel-caption">
        キャプション
      </div>
    </div>
    <div class="item">
      <img src="画像URL" alt="">
      <div class="carousel-caption">
        キャプション
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" 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-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
	</div>

</body>
</html>

ここでclassの説明

  • carousel-indicators:真ん中下の丸い点
  • carousel-inner:画像のエリア
  • item:画像(activeとついたところが最初に表示される)
  • carousel-control:右左アイコンの箇所

注意点

デフォルトでは5秒でスライド
複数箇所でスライダーを使用する時はidを変更する
画像のホバー時に一時停止する

スライド時間の変更方法

data-interval=”秒数”


<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="秒数×1000">

*デフォルト:5000 = 5秒
をdivに追記する

Bootstrapのjsフォルダにあるbootstrap.jsのintervalの数値から変更可能です。

$('.carousel').carousel({
  interval: 5000,
  pause: "hover",
  wrap: true,
  keybord: true
});

補足説明

  • interval:スライドのスピード(デフォルト:5秒)
  • pause:hoverでスライダーが止まる
  • wrap:カルーセルを循環させる(false:させない)
  • keyboard:キーボードイベントに反応する(false:させない)

画像を追加したい時は、
「<!– Indicators –>」のところに

<li data-target="#carousel-example-generic" data-slide-to=“数値"></li>

これを追加して、数値のところをスライダーの数に応じて変更します。

そして、divのclass=“item”の箇所を追加
この部分

<div class="item active">
      <img src="画像URL" alt="">
      <div class="carousel-caption">
        キャプション
      </div>
    </div>

以上でスライダーの完成です。

ここまでくると、あとはCSSで簡単に編集できるので、デフォルト表示が嫌な場合は必要に応じてカスタマイズしてください。

もちろんレスポンシブなので、デフォルトでもスマホでいい感じに表示されます。



Posted

in

Tags: