bootstrap 3 轮播图组件(Carousel) 获取当前显示图片的索引

官方文档

Events
Bootstrap’s carousel class exposes two events for hooking into carousel functionality.

Both events have the following additional properties:

direction : The direction in which the carousel is sliding (either “left” or “right”).
relatedTarget: The DOM element that is being slid into place as the active item.

有两个回调方法
Event Type Description
slide.bs.carousel This event fires immediately when the slide instance method is invoked.
slid.bs.carousel This event is fired when the carousel has completed its slide transition.

两个回调方法都可以使用

$(选择器).on('slide.bs.carousel', function (obj) {
        // 当前轮播索引
        var index = $(this).find('.item').index(obj.relatedTarget);
        console.log("索引:" + index);
 });

此方法针对bootstrap 3使用
网上的.getActiveIndex()方法,适用于bootsrap v3.0.2,如果出现无法获取的情况可以试试我这个方法


bootstrap 3 轮播图组件(Carousel) 获取当前显示图片的索引
https://yangxj96.github.io/2019/06/FE/b003/
作者
yangxj96
发布于
2019年6月19日
许可协议