Problem on adding Bootstrap carousel component

252 views Asked by At

I'am praticing and learning about bootstrap and at this moment i am trying to adding a carousel in my website.

I picked a example from Bootstrap that haves on the first DIV, the id="carouselExampleSlidesOnly", with this ID, the carousel works perfectly.

But i want to remove the "carouselExampleSlidesOnly" and change for another ID name. When i do that changing to "myCarousel", the code stop working.

I tried to add a javascript code with the ID name, but even with that, the carousel not work, and shows a static image.

<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

There are 1 answers

0
Pheonix2105 On

I believe the problem you are having is with

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

You are passing null as a parameter when it expects a string or boolean value, you can see this error from the console.

enter image description here

(Ctrl + shift + I) on Chrome

It should be

$('#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>