小码哥的IT人生

Bootstrap 5 轮播

Bootstrap 2023-08-10 09:26:54小码哥的IT人生shichen

Bootstrap 5 轮播

旋转木马 / 幻灯片

旋转木马(轮播)是用于循环浏览元素的幻灯片:

如何创建旋转木马

下例显示了如何创建带有指示器和控件的基本轮播:

示例代码:

<!-- 轮播 -->
<div id="demo" class="carousel slide" data-bs-ride="carousel">
  <!-- 指标/点 -->
  <div class="carousel-indicators">
    <button type="button" data-bs-target="#demo" data-bs-slide-to="0" class="active"></button>
    <button type="button" data-bs-target="#demo" data-bs-slide-to="1"></button>
    <button type="button" data-bs-target="#demo" data-bs-slide-to="2"></button>
  </div>
  <!-- 幻灯片/轮播 -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="la.jpg" alt="Los Angeles" class="d-block w-100">
    </div>
    <div class="carousel-item">
      <img src="chicago.jpg" alt="Chicago" class="d-block w-100">
    </div>
    <div class="carousel-item">
      <img src="ny.jpg" alt="New York" class="d-block w-100">
    </div>
  </div>
  <!-- 左右控件/图标 -->
  <button class="carousel-control-prev" type="button" data-bs-target="#demo" data-bs-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#demo" data-bs-slide="next">
    <span class="carousel-control-next-icon"></span>
  </button>
</div>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<!-- 轮播 -->
<div id="demo" class="carousel slide" data-bs-ride="carousel">
  <!-- 指标/点 -->
  <div class="carousel-indicators">
    <button type="button" data-bs-target="#demo" data-bs-slide-to="0" class="active"></button>
    <button type="button" data-bs-target="#demo" data-bs-slide-to="1"></button>
    <button type="button" data-bs-target="#demo" data-bs-slide-to="2"></button>
  </div>
  <!-- 幻灯片/轮播 -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="/i/photo/shanghai-1.jpg" alt="Shanghai" class="d-block" style="width:100%">
    </div>
    <div class="carousel-item">
      <img src="/i/photo/beijing.jpg" alt="Beijing" class="d-block" style="width:100%">
    </div>
    <div class="carousel-item">
      <img src="/i/photo/wuhan.jpg" alt="Wuhan" class="d-block" style="width:100%">
    </div>
  </div>
  <!-- 左右控件/图标 -->
  <button class="carousel-control-prev" type="button" data-bs-target="#demo" data-bs-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#demo" data-bs-slide="next">
    <span class="carousel-control-next-icon"></span>
  </button>
</div>
<div class="container-fluid mt-3">
  <h3>轮播实例</h3>
  <p>本例演示如何创建带有指示器和控件的轮播。</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

例子解释

对上例中每个类的作用的描述:

描述
.carousel 创建轮播。
.carousel-indicators 为轮播添加指示器。它们是每张幻灯片底部的小点。
表示轮播中有多少张幻灯片,以及用户当前正在查看哪张幻灯片。
.carousel-inner 将幻灯片添加到轮播。
.carousel-item 规定每张幻灯片的内容。
.carousel-control-prev 向轮播添加一个向左(上一个)按钮,允许用户在幻灯片之间回退。
.carousel-control-next 向轮播添加一个向右(下一个)按钮,允许用户在幻灯片之间前进。
.carousel-control-prev-icon 与 .carousel-control-prev 一起使用,创建“上一个”按钮。
.carousel-control-next-icon 与 .carousel-control-next 一起使用,创建“下一个”按钮。
.slide 从一个项目滑到下一个项目时添加 CSS 过渡和动画效果。
如果您不想要这种效果,请删除此类。

为幻灯片添加标题

请在每个 <div class="carousel-item"> 内的 <div class="carousel-caption"> 中添加元素,为每张幻灯片创建标题:

示例代码:

<div class="carousel-item">
  <img src="beijing.jpg" alt="Beijing">
  <div class="carousel-caption">
    <h3>Beijing</h3>
    <p>Thank you, Beijing!</p>
  </div>
</div>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<!-- 轮播 -->
<div id="demo" class="carousel slide" data-bs-ride="carousel">
  <!-- Indicators/dots -->
  <div class="carousel-indicators">
    <button type="button" data-bs-target="#demo" data-bs-slide-to="0" class="active"></button>
    <button type="button" data-bs-target="#demo" data-bs-slide-to="1"></button>
    <button type="button" data-bs-target="#demo" data-bs-slide-to="2"></button>
  </div>
  <!-- The slideshow/carousel -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="/i/photo/shanghai-1.jpg" alt="Shanghai" class="d-block" style="width:100%">
      <div class="carousel-caption">
        <h3>Shanghai</h3>
        <p>We had such a great time in Shanghai!</p>
      </div>
    </div>
    <div class="carousel-item">
      <img src="/i/photo/beijing.jpg" alt="Beijing" class="d-block" style="width:100%">
      <div class="carousel-caption">
        <h3>Beijing</h3>
        <p>Thank you, Beijing!</p>
      </div>
    </div>
    <div class="carousel-item">
      <img src="/i/photo/wuhan.jpg" alt="Wuhan" class="d-block" style="width:100%">
      <div class="carousel-caption">
        <h3>Wuhan</h3>
        <p>We love the Reganmian!</p>
      </div>
    </div>
  </div>
  <!-- Left and right controls/icons -->
  <button class="carousel-control-prev" type="button" data-bs-target="#demo" data-bs-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#demo" data-bs-slide="next">
    <span class="carousel-control-next-icon"></span>
  </button>
</div>
<div class="container-fluid mt-3">
  <h3>轮播实例</h3>
  <p>本例演示如何创建带有指示器和控件的轮播。</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024