javascript - Bootstrap 4 Dynamic Carousel using Jquery - Stack Overflow

I'm trying to add images dynamically to a Bootstrap Carousel but it just shows them in a list (.jp

I'm trying to add images dynamically to a Bootstrap Carousel but it just shows them in a list (.jpg).

The images are National Parks Service API urls (.jpg).

Here's the HTML

<div id="carousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators"></ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner"></div>
    <!-- Controls -->
        <a class="carousel-control-prev" href="#demo" data-slide="prev">
        <span class="carousel-control-prev-icon"></span>
        </a>
    <a class="carousel-control-next" href="#demo" data-slide="next">
        <span class="carousel-control-next-icon"></span>
    </a>
</div>

and JS

$(document).ready(function(){
for(let j = 0; j < parkInfo.data[i].images.length; j++) {
  $('<div class="item"><img src="'+parkInfo.data[i].images[j].url+'" width="50%">   </div>').appendTo('.carousel-inner');
  $('<li data-target="#carousel" data-slide-to="'+j+'"></li>').appendTo('.carousel-indicators')

}
  $('.item').first().addClass('active');
  $('.carousel-indicators > li').first().addClass('active');
  $('#carousel').carousel();
});

I'm trying to add images dynamically to a Bootstrap Carousel but it just shows them in a list (https://i.sstatic/qOCBe.jpg).

The images are National Parks Service API urls (https://www.nps.gov/mon/uploads/structured_data/3C86AAE7-1DD8-B71B-0BA8BC5E5E895E98.jpg).

Here's the HTML

<div id="carousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators"></ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner"></div>
    <!-- Controls -->
        <a class="carousel-control-prev" href="#demo" data-slide="prev">
        <span class="carousel-control-prev-icon"></span>
        </a>
    <a class="carousel-control-next" href="#demo" data-slide="next">
        <span class="carousel-control-next-icon"></span>
    </a>
</div>

and JS

$(document).ready(function(){
for(let j = 0; j < parkInfo.data[i].images.length; j++) {
  $('<div class="item"><img src="'+parkInfo.data[i].images[j].url+'" width="50%">   </div>').appendTo('.carousel-inner');
  $('<li data-target="#carousel" data-slide-to="'+j+'"></li>').appendTo('.carousel-indicators')

}
  $('.item').first().addClass('active');
  $('.carousel-indicators > li').first().addClass('active');
  $('#carousel').carousel();
});
Share Improve this question asked May 15, 2018 at 6:38 Lam PhamLam Pham 391 gold badge1 silver badge4 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

Looks like you need to have a class on the item 'carousel-item' not 'item'.

https://jsfiddle/orb9945u/

$(document).ready(function(){
    for(let j = 0; j < 5; j++) {
        $('<div class="carousel-item"><img 
          src="https://loremflickr./320/240" width="50%">   
          </div>').appendTo('.carousel-inner');
        $('<li data-target="#carousel" data-slide-to="'+j+'"> 
          </li>').appendTo('.carousel-indicators')

    }

    $('.carousel-item').first().addClass('active');
    $('.carousel-indicators > li').first().addClass('active');
    $('#carousel').carousel();
});

Work for me

for(let j = 0; j < data.length; j++) {
                   $('<div class="item"><img src="'+data[j].imageName+'"></div>').appendTo('.carousel-inner');
                   $('<li data-target="#carousel" data-slide-to="'+j+'"></li>').appendTo('.carousel-indicators');
              }
              $('.item').first().addClass('active');
              $('.carousel-indicators > li').first().addClass('active');
              $('#carousel').carousel();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742416852a4439908.html

相关推荐

  • javascript - Bootstrap 4 Dynamic Carousel using Jquery - Stack Overflow

    I'm trying to add images dynamically to a Bootstrap Carousel but it just shows them in a list (.jp

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信