单击转盘项目内的按钮如何转到下一个转盘项目

时间:2019-02-21 21:05:29

标签: javascript jquery bootstrap-4

我正在尝试在Bootstrap 4中创建一个轮播,其中轮播项目中有一个下一个按钮。我要在下一个按钮上click移到下一个轮播。

<div class="container">
  <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval='false'>
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner ">
      <div class="carousel-item active">
        <button type="submit" class="btn btn-primary" id="gotoNext1">Next</button>
        <img src="https://placehold.it/900x350/e8117f/fff&text=Image%201" alt="Image 1"/>        
      </div>
      <div class="carousel-item">
        <button type="submit" class="btn btn-primary" id="gotoNext2">Next</button>
        <img src="https://placehold.it/900x350/9ACD32/fff&text=Image%202" alt="Image 2"/>
      </div>
      <div class="carousel-item">
        <img src="https://placehold.it/900x350/FF6347/fff&text=Image%203" alt="Image 3"/>
      </div>
    </div>        
  </div>
</div>

请,有人可以帮助我使用Javascriptjquery来实现此功能吗?

我可以使用轮播指示器来前进或后退,但是我想使用按钮前进。

有关更多详细信息,请参见jsfiddle link ...

1 个答案:

答案 0 :(得分:1)

您可以使用JQuery中的.carousel("next")

$("button").click(() => $(".carousel").carousel("next"));
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="container">

  <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval='false'>
  
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
       <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner ">
      <div class="carousel-item active">
        <button type="submit" class="btn btn-primary " id="gotoNext1">
          Next
        </button>
        <img src="https://placehold.it/900x350/e8117f/fff&text=Image%201" alt="Image 1"/>
      </div>
      <div class="carousel-item">
        <button type="submit" class="btn btn-primary " id="gotoNext2">
          Next
        </button>
        <img src="https://placehold.it/900x350/9ACD32/fff&text=Image%202" alt="Image 2"/>
      </div>
      <div class="carousel-item">
        <img src="https://placehold.it/900x350/FF6347/fff&text=Image%203" alt="Image 3"/>
      </div>
    </div>

  </div>

</div>