我正在使用Bootstrap 3创建一个站点,并且我试图在轮播中的某些幻灯片下方显示不同的文本,当它们显示在屏幕上时。我的代码如下...
<div class="row img-slider">
<div class="col-md-10 col-md-offset-1">
<div id="sticksCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#sticksCarousel" data-slide-to="0" class="active"></li>
<li data-target="#sticksCarousel" data-slide-to="1"></li>
<li data-target="#sticksCarousel" data-slide-to="2"></li>
</ol>
<section class="carousel-inner">
<div class="item active"><img class="image1" src="img/slide1.png" alt="blah" style="width:100%;"></div>
<div class="item"><img class="image2" src="img/slide2.png" alt="blah" style="width:100%;"></div>
<div class="item"><img src="img/slide3.png" alt="blah" style="width:100%;"></div>
</section><!--carousel-inner-->
<a href="#sticksCarousel" class="left carousel-control" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"> </span></a> <a href="#sticksCarousel" class="right carousel-control" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
</div><!--sticksCarousel-->
<p id="sticksCarouselMessage">HEY!</p>
</div>
JavaScript:
<script type = "text/javascript">
$(document).ready(function () {
$('.carousel').carousel({
interval: 4000
});
$('#sticksCarousel').on('slid.bs.carousel', function () {
if ($('div.active img.image1')) {
$("#sticksCarouselMessage").text("It worked for Image 1 class!");
}
if ($('div.active img.image2')) {
$("#sticksCarouselMessage").text("It worked for Image 2 class!");
} else {
$("#sticksCarouselMessage").text("");
}
});
});
</script>
我正在尝试使用 JQuery 选择每个单独的图像并在#sticksCarouselMessage
. 我知道这可能不是最有效的方法,如果您有任何建议,请告诉我。请记住,我是 jQuery 新手,但我正在努力学习。谢谢!