如何在Bootstrap Carousel上获取多个图像每个项目

时间:2016-10-31 16:47:57

标签: twitter-bootstrap twitter-bootstrap-3

Bootstrap Carousel项目上的多图像

如您所知,BootStrap Carousel支持为

上的每个项目渲染一个图像
<div class="item">
  <img src="http://placehold.it/1920x1080" alt="...">
  <div class="carousel-caption">
    This is Regular Item with One Image
  </div>
</div>

但我需要在每个item中加载和叠加几个png图像,如

<div class="item">
  <img src="http://placehold.it/1920x1080" alt="...">
  <img src="http://placehold.it/200x600" alt="...">
  <img src="http://placehold.it/900x450" alt="...">
  <div class="carousel-caption">
    This is Overlaid Item with Multi Images
  </div>
</div>

我现在得到的是

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="http://placehold.it/1920x1080/ffff80" alt="...">
      <img src="http://placehold.it/200x600/ff0000" alt="...">
      <img src="http://placehold.it/900x450/000000" alt="...">
      <div class="carousel-caption">
        This is Overlaied Item with Multi Images
      </div>
    </div>
    <div class="item">
      <img src="http://placehold.it/1920x1080/ffff80" alt="...">
      <img src="http://placehold.it/200x600/ff0000" alt="...">
      <img src="http://placehold.it/900x450/000000" alt="...">
      <div class="carousel-caption">
        This is Overlaied Item with Multi Images
      </div>
    </div>	
  </div>
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我不知道这是你一直在寻找的,但我写了简单的jQuery脚本,它会在你的轮播中交换图像。我知道它可以做得更好,但我从jQuery开始

    var main = function(){
    $('.item.active img')
            .click(function(){
                var imgSource = ['http://placehold.it/900x450/000000', "http://placehold.it/200x600/ff0000", "http://placehold.it/1920x1080/ffff80"];
                var currImg = $(this).attr('src');
                //if currImg === imgSource[0] then currImg = imgSource[1]
                if (currImg === imgSource[0]) {
                    currImg = imgSource[1];
                    $(this).attr('src',currImg);
                }
                else if(currImg === imgSource[1]){
                    currImg = imgSource[2];
                    $(this).attr('src',currImg);
                }
                else if (currImg === imgSource[2]) {
                    currImg = imgSource[0];
                    $(this).attr('src',currImg);
                }

            });
};
$(document).ready(main);

点击它们时,它会在轮播中交换图像。试图用.each()方法做到这一点,但无法让它工作,所以在这里。我希望我帮助过你。