Bootstrap Carousel Image 滑块,我怎样才能让它工作?

Posted

技术标签:

【中文标题】Bootstrap Carousel Image 滑块,我怎样才能让它工作?【英文标题】:Bootstrap Carousel Image slider, how can I make it works? 【发布时间】:2021-09-27 22:09:18 【问题描述】:

我需要这方面的帮助。这是我第一次使用引导程序,我已经厌倦了。我想制作 3 张每次点击都会滑动和更改的图像。我在 Boostrap 网站上找到了这段代码,但它不起作用。我正在使用他们的链接和脚本。

请帮助我。对不起我的英语

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div id = "slides" class = "carousel slide" data-ride = "carousel">
        <ul class = "carousel-indicators">
            <li data-target="#slides" data-slide-to="0" class ="active"></li>
            <li data-target="#slides" data-slide-to="1"></li>
            <li data-target="#slides" data-slide-to="2"></li>
        </ul>
        <div class = "carousel-inner">
            <div class = "carousel-item active">
                <img src = "img/img1.jpg">
                <div class = "carousel-caption">
                    <h1 class = "display-2">Test</h1>
                    <h3>Testing of the test</h3>
                    <button type = "button" class = "btn btn-outline-light btn-lg">tester</button>
                    <button type = "button" class = "btn btn-primary btn-lg">Jop</button>
                </div>
            </div>
            <div class = "carousel-item">
                <img src = "img/img2.jpg">
            </div>
            <div class = "carousel-item">
                <img src = "img/img3.jpg">
            </div>
        </div>
    </div>

【问题讨论】:

【参考方案1】:

您添加了顺序错误的 JS 文件(jquery 必须在 bootstrap 文件之前!)并且您使用了 Bootstrap 4 语法但调用了版本 5 资源

<html>
  <head>
    <title></title>
    <meta content="">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <style>
        #slides margin: 0 auto;
        #slides, .carousel-inner, .carousel-item
            height: 400px; width: 600px;
        

    </style>
  </head>
  <body>

    <div id="slides" class="carousel slide" data-ride="carousel">
        <ul class="carousel-indicators">
            <li data-bs-target="#slides" data-bs-slide-to="0" class="active"></li>
            <li data-bs-target="#slides" data-bs-slide-to="1"></li>
            <li data-bs-target="#slides" data-bs-slide-to="2"></li>
        </ul>
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img src="https://cdn.pixabay.com/photo/2020/02/11/16/25/manarola-4840080_960_720.jpg" class="d-block w-100">
                <div class="carousel-caption">
                    <h1 class="display-2">Test</h1>
                    <h3>Testing of the test</h3>
                    <button type="button" class="btn btn-outline-light btn-lg">tester</button>
                    <button type="button" class="btn btn-primary btn-lg">Jop</button>
                </div>
            </div>
            <div class="carousel-item">
                <img src="https://cdn.pixabay.com/photo/2020/03/24/20/40/manarola-4965450_960_720.jpg" class="d-block w-100">
            </div>
            <div class="carousel-item">
                <img src="https://cdn.pixabay.com/photo/2016/02/19/11/18/manarola-1209627_960_720.jpg" class="d-block w-100">
            </div>
        </div>

        <button class="carousel-control-prev" type="button" data-bs-target="#slides" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Previous</span>
        </button>
        <button class="carousel-control-next" type="button" data-bs-target="#slides" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Next</span>
        </button>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
  </body>
</html>

【讨论】:

【参考方案2】:

您还没有添加下一步和预览按钮,请尝试使用此代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
    rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
    crossorigin="anonymous"
    />
  </head>
  <body>

    <div id = "slides" class = "carousel slide" data-ride = "carousel" style="height: 100;">

      <div class = "carousel-inner">
          <div class = "carousel-item active">
            <img src = "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg" class="d-block w-100">
            <div class = "carousel-caption">
                  <h1 class = "display-2">Test</h1>
                  <h3>Testing of the test</h3>
                  <button type = "button" class = "btn btn-outline-light btn-lg">tester</button>
                  <button type = "button" class = "btn btn-primary btn-lg">Jop</button>
              </div>
          </div>
          <div class = "carousel-item">
              <img src = "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg" class="d-block w-100">
          </div>
          <div class = "carousel-item">
              <img src = "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg" class="d-block w-100">
          </div>
        </div>
        <button class="carousel-control-prev" type="button" data-bs-target="#slides" data-bs-slide="prev">
          <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          <span class="visually-hidden">Previous</span>
        </button>
        <button class="carousel-control-next" type="button" data-bs-target="#slides" data-bs-slide="next">
          <span class="carousel-control-next-icon" aria-hidden="true"></span>
          <span class="visually-hidden">Next</span>
        </button>
  </div>

    <script
      src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
      integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"
      integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

【讨论】:

以上是关于Bootstrap Carousel Image 滑块,我怎样才能让它工作?的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 轮播(Carousel)插件

Bootstrap轮播图的切换按钮如何制作?

bootstrap图片轮播插件carousel

Bootstrap 轮播(Carousel)插件

bootstrap轮播图carousel插件

Bootstrap 4 Carousel 响应式(图像和文本)