引导轮播不显示图像

时间:2018-07-25 13:43:21

标签: javascript twitter-bootstrap carousel

我正在尝试制作一个轮播,可浏览图像文件夹中的图像。脚本会根据显示的图像自动将项目添加到轮播中。这些图像被标记为1、2、3等。

但是无论我尝试什么,轮播都不会显示图像。我试过将图像转换为其他格式。这些图像最初是JFIF。

这是我的代码:

<!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>

new.js文件具有将图像加载到轮播中的脚本。其作用的基本摘要是:

'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';
}

1 个答案:

答案 0 :(得分:1)

您错过了几件事:

  1. 您需要将library(data.table) dt <- data.table(df) dt[ , cond1 := sum(date == "01-07-2018" & result == "sub") > 0, by=custno] dt[ , cond2 := sum(date == "02-07-2018" & result == "renewal") > 0, by=custno] dt[cond1 == 1 & cond2 == 1, .(result = sum(amount)), by=custno] 类添加到active div中的第一个div
  2. 完成图像加载后,您应该初始化轮播。 carousel-inner

这是一个适合您的示例。

$('#pageCarousel').carousel({interval: 3000});
'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';
}