定制的Bootstrap图像滑块

时间:2016-05-21 17:58:57

标签: jquery twitter-bootstrap slider carousel

我是Bootstrap 3 Carousel的新手,想知道在更改图像时是否有任何方法可以显示当前的滑块编号,类似于下面的示例图像: enter image description here

您还可以注意到导航控件(左侧和右侧)位于当前幻灯片和幻灯片总数的旁边。

我使用this tutorial作为参考,并希望您可以帮助我解决我可以使用的其他资源。谢谢。

1 个答案:

答案 0 :(得分:2)

UPD。我添加了一个javascript来计算旋转木马中的幻灯片数量。

请检查结果。

$( document ).ready(function() {
  var selectIndicators = $('#myCarousel>.carousel-indicators>li');
  selectIndicators.each(function(i){
    $(this).text((i+1) + ' / ' + selectIndicators.length);
  }); 
});
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

.container {
  padding: 20px 0;
}

.carousel-inner {
  overflow: visible;
  width: 100%;
}
.carousel-inner > .item > img {
  margin: auto;
  width: 70%;
}

.carousel-caption {
  text-align: left;
  text-shadow: none;
  left: 70%;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}
.carousel-caption > h3 {
  color: #ddd;
  font-size: 18px;
  font-stretch: ultra-condensed;
  font-weight: 700;
  margin: 0 0 16px;
  text-transform: uppercase;
}
@media (min-width:  768px) { .carousel-caption > h3 { font-size: 24px; } }
@media (min-width:  992px) { .carousel-caption > h3 { font-size: 30px; } }
@media (min-width: 1200px) { .carousel-caption > h3 { font-size: 36px; } }
.carousel-caption > p {
  color: #000;
}
 
.carousel-control,
.carousel-control:hover {
  color: black;
  filter: none;
  opacity: 1;
  text-shadow: none;
  width: 30%;
}
.carousel-control.left,
.carousel-control.right {
  background-image: none;
}

.carousel-control .glyphicon-chevron-left,
.carousel-control .glyphicon-chevron-right,
.carousel-indicators li,
.carousel-indicators li.active {
  font-size: 20px;
  height: 40px;
  width: 40px;
  line-height: 40px;
}
.carousel-control .glyphicon-chevron-left,
.carousel-control .glyphicon-chevron-right,
.carousel-indicators {
  position: absolute;
  top: auto;
  bottom: 10px;
  margin: 0;
}
.carousel-control .glyphicon-chevron-left {
  left: 10px;
}
.carousel-control .glyphicon-chevron-right {
  right: 10px;
}
@media (min-width: 768px) { 
  .carousel-control .glyphicon-chevron-right {
    right: auto;
    left: -395px; /* = 10px + 4 x 40px - 70% x Container width 750px */
  }
}
@media (min-width: 992px) {
  .carousel-control .glyphicon-chevron-right {
    left: -549px; /* = 10px + 4 x 40px - 70% x Container width 970px */
  }
}
@media (min-width: 1200px) {
  .carousel-control .glyphicon-chevron-right {
    left: -689px; /* = 10px + 4 x 40px - 70% x Container width 1170px */
  }
}
.carousel-control:hover .glyphicon-chevron-left,
.carousel-control:hover .glyphicon-chevron-right {
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
}

.carousel-indicators {
  left: 50px; /* = left + width form .glyphicon-chevron-left */
  width: auto;
}
.carousel-indicators li,
.carousel-indicators li.active {
  background-color: transparent;
  border: 0;
  border-radius: 0;
  color: black;
  cursor: auto;
  display: none;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: -1px;
  text-align: center;
  text-indent: 0;
  white-space: nowrap;
}
.carousel-indicators li.active {
  display: inline-block;
}
<div class="container">
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img class="img-responsive" src="http://placehold.it/800x400/c69/?text=%20" alt="Slide 1">
        <div class="carousel-caption">
          <h3>Lorem<br>ipsum</h3>
          <p class="hidden-xs">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        </div>
      </div>
      <div class="item">
        <img class="img-responsive" src="http://placehold.it/800x400/69c/?text=%20" alt="Slide 2">
        <div class="carousel-caption">
          <h3>Lorem<br>ipsum</h3>
          <p class="hidden-xs">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        </div>
      </div>
      <div class="item">
        <img class="img-responsive" src="http://placehold.it/800x400/9c6/?text=%20" alt="Slide 3">
        <div class="carousel-caption">
          <h3>Lorem<br>ipsum</h3>
          <p class="hidden-xs">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        </div>
      </div>
    </div>
    <a class="left carousel-control" href="#myCarousel" 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="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div><!-- /.carousel -->
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

http://codepen.io/glebkema/pen/EKqZZN