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,如果出現無法獲取的情況可以試試我這個方法

寫在最後

本人爲純小白,所寫的博客也是因爲學習使用中遇到的問題進行查找資料然後解決後才編寫的,其中如果有某些地方有錯誤,或者涉及侵權的請聯繫我,

QQ : 994961015
E-mail: [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章