让 Bootstrap Carousel 幻灯片正常工作
Posted
技术标签:
【中文标题】让 Bootstrap Carousel 幻灯片正常工作【英文标题】:Getting Bootstrap Carousel slides to work 【发布时间】:2022-01-22 15:28:18 【问题描述】:我正在尝试实现引导轮播幻灯片,当传递到下一页时会显示 gif 短片。我尝试了多种方法,包括 Bootstrap 中的文档和 *** 中的答案。幻灯片只是不会传递到下一页.. 有人可以提供任何帮助吗?
<div class="row">
<div class="col-lg-6">
<h2>About me</h2>
<ul style="list-style-type:none; text-align: left;">
<li>languages: Java, JS, html, CSS</li>
<li>Frameworks: Bootstrap, e</li>
<li>Tools:</li>
<li>Editor Journey:</li>
<li>Operating System:</li>
</ul>
</div>
<div class="col-lg-6">
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<h2>Project 1</h2>
<img class="project1" src="videos/QuickGame.gif" >
<em>Shooter Game, Java</em>
</div>
<div class="carousel-item">
<h2>Project 2</h2>
<img class="project2" src="videos/DiceGame.gif" >
<em>Dice Game, javascript</em>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
【问题讨论】:
任何答案都解决了您的问题吗? 【参考方案1】:在官方docs 中,所有幻灯片行为、控件和指示器的Carousel 组件都需要 JavaScript。我们可以阅读更多内容:
JS
我们的许多组件都需要使用 JavaScript 才能运行。 具体来说,它们需要 jQuery、Popper 和我们自己的 JavaScript 插件。我们使用 jQuery 的 slim build,但完整版也是 支持。
将以下其中一个 s 放在页面末尾附近,对 在结束标记之前,启用它们。 jQuery必须来 首先是 Popper,然后是我们的 JavaScript 插件。
您的代码(建议您更加努力),链接正确连接,bootstrap@4.6.1
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="row">
<div class="col-lg-6">
<h2>About me</h2>
<ul style="list-style-type:none; text-align: left;">
<li>languages: Java, JS, HTML, CSS</li>
<li>Frameworks: Bootstrap, e</li>
<li>Tools:</li>
<li>Editor Journey:</li>
<li>Operating System:</li>
</ul>
</div>
<div class="col-lg-6">
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<h2>Project 1</h2>
<img class="project1" src="https://images.unsplash.com/photo-1639975721073-3ef4e6d2d4cf?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwzNXx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60" >
<em>Shooter Game, Java</em>
</div>
<div class="carousel-item">
<h2>Project 2</h2>
<img class="project2" src="https://images.unsplash.com/photo-1640043285014-3d1e095b3ebe?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw1fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60" >
<em>Dice Game, JavaScript</em>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>
</body>
</html>
【讨论】:
【参考方案2】:正如官方bootstrap-4
documentation所说:
我们的许多组件都需要使用 JavaScript 才能运行。具体来说,它们需要 jQuery、Popper.js 和我们自己的 JavaScript 插件。将以下 s 放在页面末尾附近,就在结束标记之前,以启用它们。 jQuery 必须先出现,然后是 Popper.js,然后是我们的 JavaScript 插件。
您需要在您的</body>
之前包含这些<script>
:
<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>
【讨论】:
【参考方案3】:你在结束</body>
标签之前包含了下面的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>
来源:https://getbootstrap.com/docs/4.0/getting-started/introduction/
【讨论】:
以上是关于让 Bootstrap Carousel 幻灯片正常工作的主要内容,如果未能解决你的问题,请参考以下文章
Bootstrap Carousel 和我正在构建的主题的问题
Bootstrap 4 Carousel:每张幻灯片上的单独数据间隔