添加Bootstrap轮播组件

时间:2019-01-19 23:57:26

标签: bootstrap-4 bootstrap-carousel

我很热衷于了解引导程序,目前我正试图在我的网站中添加一个轮播。

我从Bootstrap中选择了一个示例,该示例在第一个DIV上具有id =“ carouselExampleSlidesOnly”,并具有此ID,该旋转木马可以完美地工作。

但是我想删除“ carouselExampleSlidesOnly”并更改为另一个ID名称。当我将其更改为“ myCarousel”时,代码停止工作。

我尝试添加ID名称的javascript代码,但即使这样,轮播也无法正常工作,并显示静态图片。

<script>
$('#myCarousel').carousel({
  interval: 3000,
  pause: null,
})
</script>

   <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="3000" data-pause="null">
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="d-block w-100" src="assets/images/slide1.png" alt="First slide">
            <div class="container">
                <div class="slider-text">
                    <h2>Lorem Ipsum is simply dummy text.</h2>
                    <p>ndustry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer.</p>
                </div>

            </div>
        </div>

        <div class="carousel-item">
            <img class="d-block w-100" src="assets/images/slide2.png" alt="Second slide">
            <div class="container">
                <div class="slider-text">
                    <h2>Lorem Ipsum is simply dummy text.</h2>
                    <p>ndustry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer.</p>
                </div>

            </div>
        </div>

    </div>
    <a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

1 个答案:

答案 0 :(得分:0)

我相信您遇到的问题

$('#myCarousel').carousel({
  interval: 3000,
  pause: null,
})

当期望字符串或布尔值时,您将传递null作为参数,您可以从控制台看到此错误。

enter image description here

(在Chrome上按Ctrl + Shift + I)

应该是

$('#myCarousel').carousel({
  interval: 3000,
  pause: false,
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>

<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="3000" data-pause="null">
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="d-block w-100" src="https://via.placeholder.com/150" alt="First slide">
            <div class="container">
                <div class="slider-text">
                    <h2>Lorem Ipsum is simply dummy text.</h2>
                    <p>ndustry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer.</p>
                </div>

            </div>
        </div>

        <div class="carousel-item">
            <img class="d-block w-100" src="https://via.placeholder.com/150" alt="Second slide">
            <div class="container">
                <div class="slider-text">
                    <h2>Lorem Ipsum is simply dummy text.</h2>
                    <p>ndustry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer.</p>
                </div>

            </div>
        </div>

    </div>
    <a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>