Bootstrap Carousel无法正常工作(在页面上显示所有元素)

时间:2018-10-25 07:12:21

标签: javascript html css bootstrap-4 carousel

我正在尝试创建一个轮播。我已经创建了结构,但只希望出现五个块,然后在单击箭头后出现第六个块。我该怎么办?

我的方法 :(采用from here

img{
  border: 1px solid red;
  width: 100px;
  height: 100px;
  display: block;
}
ul{
  display: inline-block;
}
li{
  list-style-type: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="item active">
      <!----------->
      <!-- CTA 1 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 1</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 2 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 2</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 3 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 3</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 4 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 4</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 5 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 5</span>
        </li>
      </ul>
      <!----------->
      <!----------->

      <!-- TO APPEAR AFTER CONTROL CLICK -->

      <!----------->
      <!-- CTA 6 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 6</span>
        </li>
      </ul>
    </div>
  </div>
  <!--=====================-->
  <!--== ON ARROW DISPLAY -==>  
  <!--=====================-->


  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

如您所见,当前,它最初显示全部六个。不确定所链接的演示与我的方法之间有什么区别(当然还有结构)。

3 个答案:

答案 0 :(得分:1)

那是因为您将所有图像都保存在一个<div class="item">

以这种方式将它们保存在单独的div中:

<div class="item active">cat 1</div>
<div class="item">cat 2</div>
<div class="item">cat 3</div>
<div class="item">cat 4</div>
<div class="item">cat 5</div>
<div class="item">cat 6</div>

img {
  border: 1px solid red;
  width: 100px;
  height: 100px;
  display: block;
}

ul {
  display: inline-block;
}

li {
  list-style-type: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="item active">
      <!----------->
      <!-- CTA 1 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="" />
          <span>Test 1</span>
        </li>
      </ul>
    </div>
    <div class="item">
      <!----------->
      <!-- CTA 2 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="" />
          <span>Test 2</span>
        </li>
      </ul>
    </div>
    <div class="item ">
      <!----------->
      <!-- CTA 3 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="" />
          <span>Test 3</span>
        </li>
      </ul>
    </div>
    <div class="item ">
      <!----------->
      <!-- CTA 4 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="" />
          <span>Test 4</span>
        </li>
      </ul>
    </div>
    <div class="item ">
      <!----------->
      <!-- CTA 5 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="" />
          <span>Test 5</span>
        </li>
      </ul>
    </div>
    <div class="item ">
      <!----------->
      <!----------->

      <!-- TO APPEAR AFTER CONTROL CLICK -->

      <!----------->
      <!-- CTA 6 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="" />
          <span>Test 6</span>
        </li>
      </ul>
    </div>
  </div>
  <!--=====================-->
  <!--== ON ARROW DISPLAY -==>  
  <!--=====================-->


  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

答案 1 :(得分:0)

这应该像您预期的那样工作。添加了一个类为item的div。

img{
  border: 1px solid red;
  width: 100px;
  height: 100px;
  display: block;
}
ul{
  display: inline-block;
}
li{
  list-style-type: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="item active">
      <!----------->
      <!-- CTA 1 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 1</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 2 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 2</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 3 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 3</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 4 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 4</span>
        </li>
      </ul>
      <!----------->
      <!-- CTA 5 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 5</span>
        </li>
      </ul>
    </div>
    <div class="item">
      <!----------->
      <!----------->

      <!-- TO APPEAR AFTER CONTROL CLICK -->

      <!----------->
      <!-- CTA 6 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src=""/>
          <span>Test 6</span>
        </li>
      </ul>
    </div>
  </div>
  <!--=====================-->
  <!--== ON ARROW DISPLAY -==>  
  <!--=====================-->


  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

答案 2 :(得分:0)

img{
  border: 1px solid red;
  width: 100px;
  height: 100px;
  display: block;
}
ul{
  display: inline-block;
}
li{
  list-style-type: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="item active">
      <!----------->
      <!-- CTA 1 -->
      <!----------->
      <ul class="clearfix">
        <li>
          <img src="https://picsum.photos/200/200?random"/>
          <span>Test 1</span>
        </li>
      </ul>
      </div>
      <!----------->
      <!-- CTA 2 -->
      <!----------->
       <div class="item">
      <ul class="clearfix">
        <li>
          <img src="https://picsum.photos/200/200?random"/>
          <span>Test 2</span>
        </li>
      </ul>
      <div>
      <!----------->
      <!-- CTA 3 -->
      <!----------->
       <div class="item">
      <ul class="clearfix">
        <li>
          <img src="https://picsum.photos/200/200?random"/>
          <span>Test 3</span>
        </li>
      </ul>
      </div>
      <!----------->
      <!-- CTA 4 -->
      <!----------->
       <div class="item">
      <ul class="clearfix">
        <li>
          <img src="https://picsum.photos/200/200?random"/>
          <span>Test 4</span>
        </li>
      </ul>
      </div>
      <!----------->
      <!-- CTA 5 -->
      <!----------->
       <div class="item">
      <ul class="clearfix">
        <li>
          <img src="https://picsum.photos/200/200?random"/>
          <span>Test 5</span>
        </li>
      </ul>
      </div>
      <!----------->
      <!----------->

      <!-- TO APPEAR AFTER CONTROL CLICK -->

      <!----------->
      <!-- CTA 6 -->
      <!----------->
       <div class="item">
      <ul class="clearfix">
        <li>
          <img src="https://picsum.photos/200/200?random"/>
          <span>Test 6</span>
        </li>
      </ul>
      </div>
    </div>
  </div>
  <!--=====================-->
  <!--== ON ARROW DISPLAY -==>  
  <!--=====================-->


  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

所有6个字都出现在这里。有什么问题吗?