我正在寻找如何创建带有缩略图的 Bootstrap 轮播,我发现了这个: http://jsfiddle.net/xuhP9/67/
$('#myCarousel').carousel({
interval: 4000
});
$('[id^=carousel-selector-]').click(function () {
var id_selector = $(this).attr("id");
var id = id_selector.substr(id_selector.length - 1);
id = parseInt(id);
$('#myCarousel').carousel(id);
$('[id^=carousel-selector-]').removeClass('selected');
$(this).addClass('selected');
});
$('#myCarousel').on('slid', function (e) {
var id = $('.item.active').data('slide-number');
id = parseInt(id);
$('[id^=carousel-selector-]').removeClass('selected');
$('[id^=carousel-selector-' + id + ']').addClass('selected');
});
效果很好,但是,我需要在一个页面中有多个轮播,而且我不太确定如何实现这一点。我厌倦了将 id 选择器切换为类选择器,这样我就可以创建多个选择器。但我不确定如何实际修复 JS 功能以使其正常工作,因为它们似乎一起被蒙蔽了。
基本上,这就是我想要实现的目标:http://jsfiddle.net/xuhP9/70/但无需为我创建的每个独立轮播重复 JS。
提前致谢!
最佳答案
此方法要求您的轮播具有 ID = myCarousel1、myCarousel2 等。
相应轮播的选择器为 carousel-selector1-1、carousel-selector1-2 ... 和 carousel-selector2-1、carousel-selector2-2
更新的 fiddle :http://jsfiddle.net/xuhP9/77/
$('.customCarousel').carousel({
interval: 4000
});
// handles the carousel thumbnails
$('[id^=carousel-selector]').click(function () {
var id_selector = $(this).attr("id");
var id = id_selector.substr(id_selector.length - 1);
id = parseInt(id);
var parent = $(this).closest('ul').data('carousel');
$('#myCarousel' + parent).carousel(id);
$('[id^=carousel-selector' + parent +'-]').removeClass('selected');
$(this).addClass('selected');
});
// when the carousel slides, auto update
$('.customCarousel').on('slid', function (e) {
var cont = $(this).data('carousel');
var id = $('#myCarousel'+ cont +' .item.active').data('slide-number');
id = parseInt(id);
$('[id^=carousel-selector' +cont+'-]').removeClass('selected');
$('[id^=carousel-selector'+cont+'-' + id + ']').addClass('selected');
});
关于javascript - 带缩略图的 Bootstrap 轮播(多个轮播),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25752187/