轮播暂停问题

时间:2019-02-14 18:31:33

标签: jquery twitter-bootstrap

我正在关注以下论坛,并尝试在我的代码中实现暂停功能。

http://jsfiddle.net/t9v1dwey/

当我将代码放入HTML时,它如下所示。

enter image description here

我已经在正弦中描述了代码

$(function() {
  /* Initialize Carousel */
  var paused = 0;
  $('#myCarousel').carousel({
    interval: 1000,
    pause: 0
  });

  /* Play trigger */
  $('#toggleCarousel').click(function() {
    var state = (paused) ? 'cycle' : 'pause';
    paused = (paused) ? 0 : 1;
    $('#myCarousel').carousel(state);
    $(this).find('i').toggleClass('fa-play fa-pause');
  });
});
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
  width: 100%;
  margin: auto;
}

.img-title {
  position: relative;
  background-color: #fff;
  color: #000;
  width: 100%;
  text-align: center;
  opacity: 0.43;
  filter: alpha(opacity=43);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <br>
  <button id="toggleCarousel"><i class="fa fa-pause"></i></button>
  <div id="myCarousel" class="carousel slide">
    <!-- 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>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <div class="img-title">Chania This is an Image</div>
        <img src="http://www.w3schools.com/bootstrap/img_chania.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <div class="img-title">Some other text discription goes at the top that goes a distance across the top</div>
        <img src="http://www.w3schools.com/bootstrap/img_chania2.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <div class="img-title">an Image of Flower</div>
        <img src="http://www.w3schools.com/bootstrap/img_flower.jpg" alt="Flower" width="460" height="345">
      </div>

      <div class="item">
        <div class="img-title">Keep this to the Description</div>
        <img src="http://www.w3schools.com/bootstrap/img_flower2.jpg" alt="Flower" width="460" height="345">
      </div>
    </div>
    <!-- Left and right controls -->
    <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>
</div>

预期结果应与以下相同。

http://jsfiddle.net/t9v1dwey/

2 个答案:

答案 0 :(得分:0)

嗯...看来您还没有提供一些重要资源

您需要包括Bootstrap,jQuery和font-awsome。

答案 1 :(得分:0)

bootstrap.min.cssfontawesome这些库在您的代码中丢失了

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

$(function() {
  /* Initialize Carousel */
  var paused = 0;
  $('#myCarousel.carousel').carousel({
    interval: 1000,
    pause: 0
  });

  /* Play trigger */
  $('#toggleCarousel').click(function() {
    var state = (paused) ? 'cycle' : 'pause';
    paused = (paused) ? 0 : 1;
    $('#myCarousel').carousel(state);
    $(this).find('i').toggleClass('fa-play fa-pause');
  });
});
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
  width: 100%;
  margin: auto;
}

.img-title {
  position: relative;
  background-color: #fff;
  color: #000;
  width: 100%;
  text-align: center;
  opacity: 0.43;
  filter: alpha(opacity=43);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="container">
  <br>
  <button id="toggleCarousel"><i class="fa fa-pause"></i></button>
  <div id="myCarousel" class="carousel slide">
    <!-- 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>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <div class="img-title">Chania This is an Image</div>
        <img src="http://www.w3schools.com/bootstrap/img_chania.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <div class="img-title">Some other text discription goes at the top that goes a distance across the top</div>
        <img src="http://www.w3schools.com/bootstrap/img_chania2.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <div class="img-title">an Image of Flower</div>
        <img src="http://www.w3schools.com/bootstrap/img_flower.jpg" alt="Flower" width="460" height="345">
      </div>

      <div class="item">
        <div class="img-title">Keep this to the Description</div>
        <img src="http://www.w3schools.com/bootstrap/img_flower2.jpg" alt="Flower" width="460" height="345">
      </div>
    </div>
    <!-- Left and right controls -->
    <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>
</div>