MythicalCode_
MythicalCode_

Reputation: 85

Bootstrap carousel not showing images

I'm trying to make a carousel which browses through the images in an image folder. A script automatically adds the items to the carousel based on the images present. The images are labelled 1, 2, 3, and so on.

But no matter what I try, the carousel doesn't show the image. I've tried converting the images to different format. The images are originally JFIF.

Here's my code:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <title>Edexcel International GCSE (9-1) Human Biology Student Book | Scotch's Pirated Book Reader (PBR) v3.6</title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body onLoad="load()" style="margin-top: 10px;">
        <div class="container">
            <div style="text-align: center; line-height: 2px;">
                <h4>Image browser</h4>
                <p>Coded by someone</p>
            </div><br /><br />
            <div id="pageCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
                <div class="carousel-inner" role="listbox" id="carousel-inner">
                    <!-- GONNA BE FILLED BY THE SCRIPT -->
                </div>
                <a class="carousel-control-prev" href="#pageCarousel" role="button" data-slide="prev">
                    <span class="carousel-control-prev-icon bg-primary" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="carousel-control-next" href="#pageCarousel" role="button" data-slide="next">
                    <span class="carousel-control-next-icon bg-primary" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>

        <script src="js/jquery-3.3.1.slim.min.js"></script>
        <script src="js/popper.min.js"></script>
        <script src="js/bootstrap.js"></script>
        <script src="js/new.js"></script>
    </body>
</html>

The new.js file has the script which loads the images into the carousel. A basic summary of what it does is:

'use strict';

function load()
{
    var index = 2; // The first image is named 2.jfif
    var carousel_innerHTML = "";

    while (index < 278) // There are 277 images
    {
        carousel_innerHTML = carousel_innerHTML + makeEntry(index);
        index++;
    }

    document.getElementById('carousel-inner').innerHTML = carousel_innerHTML;
}

function makeEntry(index)
{
    return '<!-- AUTO GENERATED CODE --> <div class="carousel-item"><img class="d-block mx-auto" src="assets/' + index + '.jfif" alt="' + index + '" /></div>\n';
}

Upvotes: 0

Views: 1856

Answers (1)

Andrew L
Andrew L

Reputation: 5486

You were missing a couple things:

  1. You need to add an active class to the first div in the carousel-inner div
  2. You should initialize the carousel when finished loading the images. $('#pageCarousel').carousel({interval: 3000});

Here is a working example for you.

'use strict';
// for this example
var images = ["","","https://upload.wikimedia.org/wikipedia/commons/c/c9/10150811_677994762257483_131131840_n_677994762257483.jpg", "https://upload.wikimedia.org/wikipedia/commons/1/1b/10603847_756710051052620_5858255939401577860_o_756710051052620.jpg","https://upload.wikimedia.org/wikipedia/commons/6/6a/1073115_556203904436570_702830415_o_556203904436570.jpg"]

function load() {
  var index = 2; // The first image is named 2.jfif
  var carousel_innerHTML = "";

  while (index < 5) // There are 277 images
  {
    carousel_innerHTML = carousel_innerHTML + makeEntry(index);
    index++;
  }

  document.getElementById('carousel-inner').innerHTML = carousel_innerHTML;
  $('#pageCarousel').carousel({interval: 3000});
}

function makeEntry(index) {
  var first = index === 2 ? "active" : "";    
  return '<!-- AUTO GENERATED CODE --> <div class="carousel-item ' + first + '"><img class="d-block mx-auto" src="' + images[index] + '" alt="' + index + '" /></div>\n';
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<body onLoad="load()" style="margin-top: 10px;">
  <div class="container">
    <div style="text-align: center; line-height: 2px;">
      <h4>Image browser</h4>
      <p>Coded by someone</p>
    </div><br /><br />
    <div id="pageCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
      <div class="carousel-inner" role="listbox" id="carousel-inner">
        <!-- GONNA BE FILLED BY THE SCRIPT -->
      </div>
      <a class="carousel-control-prev" href="#pageCarousel" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon bg-primary" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" href="#pageCarousel" role="button" data-slide="next">
        <span class="carousel-control-next-icon bg-primary" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
  </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

  </html>

Upvotes: 2

Related Questions