Bootstrap5 轮播
2023-06-27 16:15 更新
轮播是一个循环的幻灯片:
如何创建轮播
以下实例创建了一个简单的图片轮播效果 :
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" ></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="https://atts.w3cschool.cn/attachments/image/20230627/1687853392357832.jpeg" class="d-block" style="width:100%">
</div>
<div class="carousel-item">
<img src="https://atts.w3cschool.cn/attachments/image/20230627/1687853385601596.jpeg" class="d-block" style="width:100%">
</div>
<div class="carousel-item">
<img src="https://atts.w3cschool.cn/attachments/image/20230627/1687853376323985.jpeg" 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>
</body>
</html>
轮播图片上添加描述
在每个 <div class="carousel-item"> 内添加 <div class="carousel-caption"> 来设置轮播图片的描述文本::
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap5 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<!-- Carousel -->
<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="https://atts.w3cschool.cn/attachments/image/20230627/1687853392357832.jpeg" class="d-block" style="width:100%">
<div class="carousel-caption">
<h3>第一张图片描述标题</h3>
<p>第一张图片描述内容显示在这里!!!</p>
</div>
</div>
<div class="carousel-item">
<img src="https://atts.w3cschool.cn/attachments/image/20230627/1687853385601596.jpeg" class="d-block" style="width:100%">
<div class="carousel-caption">
<h3>第二张图片描述标题</h3>
<p>第二张图片描述内容显示在这里!!!</p>
</div>
</div>
<div class="carousel-item">
<img src="https://atts.w3cschool.cn/attachments/image/20230627/1687853376323985.jpeg" class="d-block" style="width:100%">
<div class="carousel-caption">
<h3>第三张图片描述标题</h3>
<p>第三张图片描述内容显示在这里!!!</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>
</body>
</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
|
切换图片的过渡和动画效果,如果你不需要这样的效果,可以删除这个类。 |
以上内容是否对您有帮助:
更多建议: