多个带有播放/暂停按钮的引导轮播

时间:2018-09-26 21:41:35

标签: javascript jquery html twitter-bootstrap carousel

如何修改以下代码,以使“播放/暂停”按钮仅适用于单个轮播?我希望他们两个都是独立的。是否可以将click函数与父容器链接起来?

JS:

$(function () {
       $('.carousel').carousel({
           interval: 1000,
           pause: "false"
       });
       $('.carousel .carousel-controls .play').click(function () {
           $('.carousel').carousel('cycle');
       });
       $('.carousel .carousel-controls .pause').click(function () {
           $('.carousel').carousel('pause');
       });
    });

HTML:

<div id="content">
    <div class="carousel slide" id="carouselExampleControls">
        <div class="carousel-controls">
            <a data-slide="prev" href="#carouselExampleControls" role="button">Previous</a> <a class="play" href="#" role="button">Play</a> <a class="pause" href="#" role="button">Pause</a> <a data-slide="next" href="#carouselExampleControls" role="button">Next</a>
        </div>
        <div class="carousel-inner">
            <div class="carousel-item active"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
            <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=cccccc"></div>
            <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
        </div>
    </div>

    <div class="carousel slide" id="carouselExampleControls">
        <div class="carousel-controls">
            <a data-slide="prev" href="#carouselExampleControls" role="button">Previous</a> <a class="play" href="#" role="button">Play</a> <a class="pause" href="#" role="button">Pause</a> <a data-slide="next" href="#carouselExampleControls" role="button">Next</a>
        </div>
        <div class="carousel-inner">
            <div class="carousel-item active"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
            <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=cccccc"></div>
            <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以使用closest()查找所单击按钮的上级.carousel

$(function() {
  $('.carousel').carousel({
    interval: 1000,
    pause: "false"
  });
  $('.carousel .carousel-controls .play').click(function() {
    $(this).closest('.carousel').carousel('cycle');
  });
  $('.carousel .carousel-controls .pause').click(function() {
    $(this).closest('.carousel').carousel('pause');
  });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/10.2.0/css/bootstrap-slider.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/10.2.0/bootstrap-slider.min.js"></script>

<div id="content">
  <div class="carousel slide" id="carouselExampleControls">
    <div class="carousel-controls">
      <a data-slide="prev" href="#carouselExampleControls" role="button">Previous</a> <a class="play" href="#" role="button">Play</a> <a class="pause" href="#" role="button">Pause</a> <a data-slide="next" href="#carouselExampleControls" role="button">Next</a>
    </div>
    <div class="carousel-inner">
      <div class="carousel-item active"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
      <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=cccccc"></div>
      <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
    </div>
  </div>

  <div class="carousel slide" id="carouselExampleControls">
    <div class="carousel-controls">
      <a data-slide="prev" href="#carouselExampleControls" role="button">Previous</a> <a class="play" href="#" role="button">Play</a> <a class="pause" href="#" role="button">Pause</a> <a data-slide="next" href="#carouselExampleControls" role="button">Next</a>
    </div>
    <div class="carousel-inner">
      <div class="carousel-item active"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
      <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=cccccc"></div>
      <div class="carousel-item"><img src="https://placeholdit.co//i/900x450?bg=d1e1d1"></div>
    </div>
  </div>
</div>