如何使用jQuery在幻灯片更改的单独div中显示Bootstrap 4 Carousel Caption?

时间:2018-11-21 07:58:31

标签: javascript jquery bootstrap-4 carousel

我想在轮播之外的每个幻灯片事件上显示/更改字幕。我尝试使用jQuery,但无法显示标题。如果我的解释不够清楚,我还会提供一张图片作为参考。理想情况下,我想定位箭头和指示器在图像中的位置,但是我敢肯定我可以自行解决。谢谢您的帮助!

layout

$('.carousel').carousel({
  interval: 2000
});

$('#text').html($('.active > .carousel-caption').html());
$('.carousel').on('slide.bs.carousel', function() {
  $('#text').html($('.active > .carousel-caption').html());
});
.carousel-caption {
  display: none;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">



<div id="carousel-example-captions" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-captions" data-slide-to="0" class=""></li>
    <li data-target="#carousel-example-captions" data-slide-to="1" class=""></li>
    <li data-target="#carousel-example-captions" data-slide-to="2" class=""></li>
    <li data-target="#carousel-example-captions" data-slide-to="3" class="active"></li>
  </ol>
  <div class="carousel-inner" role="listbox">
    <div class="carousel-item active">
      <img class="img-fluid" src="./img/dip.jpg" alt="First slide">
      <div class="carousel-caption">
        <h1 style="margin-bottom: 60px;" class="heading-1">Flying M Seasonings</h1>
        <h2 class="heading-2">Product Title</h2>
        <p class="sub-heading">Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia</p>
        <p><button class="btn btn-primary">Shop Now</button></p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="./img/steak.jpg" alt="Second slide">
      <div class="carousel-caption ">
        <h2 class="heading-2">Product Title</h2>
        <p class="sub-heading">Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia</p>
        <p><button class="btn btn-primary">Shop Now</button></p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="./img/salad.jpg" alt="Third slide">
      <div class="carousel-caption ">
        <h2 class="heading-2">Product Title</h2>
        <p class="sub-heading">Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia</p>
        <p><button class="btn btn-primary">Shop Now</button></p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="./img/sticks.jpg" alt="Fourth slide">
      <div class="carousel-caption ">
        <h2 class="heading-2">Product Title</h2>
        <p class="sub-heading">Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia</p>
        <p><button class="btn btn-primary">Shop Now</button></p>
      </div>
    </div>
  </div>
  <a class="carousel-control-prev" href="#carousel-example-captions" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carousel-example-captions" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

<div id="#text"></div>

0 个答案:

没有答案