我有一个由WordPress主题生成的Owl Carousel 2,并且我设置了一些类似于以下内容的自定义脚本:
owl.on('changed.owl.carousel', function() {
activeEls = $('.owl-item.active');
setCarouselCaption( activeEls[1] );
});
这会将中间图像的图像标题放置在全尺寸容器中的转盘下方。
我遇到的问题是,当我将active
项分配给activeEls
时-它是在单击之前而不是之后捕获活动项。
有什么方法可以等待以使猫头鹰轮播更新active
项,然后将其分配给activeEls
?
OR-是否可以检测到单击了Previous
或Next
按钮?目前,我只能检测到更改,而不能检测到哪个更改。
答案 0 :(得分:0)
一个.setTimeout()
短至1ms就能解决问题。
$(document).ready(function(){
var owl = $('.owl-carousel').owlCarousel({
loop:true,
margin:10,
nav:true,
items:3
});
owl.on('changed.owl.carousel', function(event) {
setTimeout(function(){
var activeEls = $('.owl-item.active').eq(1); // .eq(1) to get the "middle image out of 3 actives"
setCarouselCaption( activeEls );
},1);
});
function setCarouselCaption(el){
$(".owl-item").removeClass("target");
el.addClass("target");
}
}); // Ready
.target{
border-bottom:1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css" rel="stylesheet"/>
<div class="owl-carousel owl-theme">
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+1"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+2"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+3"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+4"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+5"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+6"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+7"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+8"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+9"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+10"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+11"></h4></div>
<div class="item"><h4><img src="http://via.placeholder.com/350x150/?text=image+12"></h4></div>
</div>