您的代码不起作用的原因是没有为 Owl Carousel 定义 onChange 回调。
有关可用选项,请参阅标题回调下的http://owlgraphic.com/owlcarousel/#customizing 。
如果您更新它以使用“afterMove”,它将在幻灯片移动后正常工作。
您可能还需要考虑使用“afterAction”,它会根据您的要求在启动、移动和更新时触发。
JS
var owl = $(".owl-carousel");
owl.owlCarousel({
loop: true,
autoplay: true,
items: 1,
animateOut: 'fadeOut',
afterMove: function (elem) {
var current = this.currentItem;
var src = elem.find(".owl-item").eq(current).find("img").attr('src');
console.log('Image current is ' + src);
}
});
编辑
在进一步阅读评论中提供的链接和 owl carousel 2 的文档后,这里是一个使用 owl carousel 2 的工作示例。请参阅此jsfiddle
与 beta 中的任何内容一样,github 问题和答案可能很快就会过时。从此处的 owl carousel 2 站点上的文档中找到解决方案
HTML
<div id="banner" class="owl-carousel">
<img src="http://www.live-on-the-edge.com/wp-content/uploads/2014/06/Sam-Tomkins-730x547.jpg" alt=""/>
<img src="http://static.guim.co.uk/sys-images/Sport/Pix/pictures/2009/6/11/1244720277422/Aussie-Rugby-League-001.jpg" alt=""/>
<img src="http://static.guim.co.uk/sys-images/Sport/Pix/pictures/2010/1/7/1262873655905/Rugby-referee-001.jpg" alt=""/>
<img src="http://static.guim.co.uk/sys-images/Sport/Pix/columnists/2011/3/3/1299187263691/football-league-premier-l-007.jpg" alt=""/>
</div>
JS
var owl = $(".owl-carousel");
owl.owlCarousel({
loop: true,
autoplay: true,
items: 1,
animateOut: 'fadeOut'
});
// jQuery method on
owl.on('changed.owl.carousel',function(property){
var current = property.item.index;
var src = $(property.target).find(".owl-item").eq(current).find("img").attr('src');
console.log('Image current is ' + src);
});