使用 2 个单独的轮播非常简单。
基本概念是您有 2 个单独的轮播:一个用于大图像,第二个用于拇指。拇指只是第二个轮播中的项目。诀窍是确保将拇指上的数据目标设置为第一个轮播的 id。
我还将“拇指”轮播设置为不自行循环。如果你想让拇指轮播周期与第一个轮播“同步”,你可以用一点 js 来做到这一点。稍后我将尝试通过一个示例对其进行更新。
HTML:
<div class="container">
<div class="row">
<div class="col-sm-6">
<div id="carousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="item active">
<img src="http://placehold.it/350x250/e8117f/fff&text=Product+Main">
</div>
<div class="item">
<img src="http://placehold.it/350x250/00ffff/000&text=Product+Image+2">
</div>
<div class="item">
<img src="http://placehold.it/350x250/ff00ff/fff&text=Product+Image+3">
</div>
<div class="item">
<img src="http://placehold.it/350x250/ffff00/000&text=Product+Image+4">
</div>
<div class="item">
<img src="http://placehold.it/350x250/e8117f/fff&text=Product+Image+5">
</div>
<div class="item">
<img src="http://placehold.it/350x250/00ffff/000&text=Product+Image+6">
</div>
<div class="item">
<img src="http://placehold.it/350x250/ff00ff/fff&text=Product+Image+7">
</div>
<div class="item">
<img src="http://placehold.it/350x250/ffff00/000&text=Product+Image+8">
</div>
</div>
</div>
<div class="clearfix">
<div id="thumbcarousel" class="carousel slide" data-interval="false">
<div class="carousel-inner">
<div class="item active">
<div data-target="#carousel" data-slide-to="0" class="thumb"><img src="http://placehold.it/100/e8117f/fff&text=Product+Main"></div>
<div data-target="#carousel" data-slide-to="1" class="thumb"><img src="http://placehold.it/100/00ffff/000&text=Product+Image+2"></div>
<div data-target="#carousel" data-slide-to="2" class="thumb"><img src="http://placehold.it/100/ff00ff/fff&text=Product+Image+3"></div>
<div data-target="#carousel" data-slide-to="3" class="thumb"><img src="http://placehold.it/100/ffff00/000&text=Product+Image+4"></div>
</div><!-- /item -->
<div class="item">
<div data-target="#carousel" data-slide-to="4" class="thumb"><img src="http://placehold.it/100/e8117f/fff&text=Product+Image+5"></div>
<div data-target="#carousel" data-slide-to="5" class="thumb"><img src="http://placehold.it/100/00ffff/000&text=Product+Image+6"></div>
<div data-target="#carousel" data-slide-to="6" class="thumb"><img src="http://placehold.it/100/ff00ff/fff&text=Product+Image+7"></div>
<div data-target="#carousel" data-slide-to="7" class="thumb"><img src="http://placehold.it/100/ffff00/000&text=Product+Image+8"></div>
</div><!-- /item -->
</div><!-- /carousel-inner -->
<a class="left carousel-control" href="#thumbcarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#thumbcarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div> <!-- /thumbcarousel -->
</div><!-- /clearfix -->
</div> <!-- /col-sm-6 -->
<div class="col-sm-6">
<h2>Products</h2>
<h3>Some product subhead</h3>
<p>Product description goes here. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>
<p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.</p>
</div> <!-- /col-sm-6 -->
</div> <!-- /row -->
</div> <!-- /container -->
CSS:
.item .thumb {
width: 25%;
cursor: pointer;
float: left;
}
.item .thumb img {
width: 100%;
margin: 2px;
}
.item img {
width: 100%;
}
编辑:
同步选项 1: 为了使项目保持同步,您可以使用 jQuery 计算主轮播中有多少项目以及拇指轮播中有多少。然后使用它来计算拇指旋转木马内主旋转木马中活动幻灯片的位置。这取决于您的主图像和拇指图像的顺序相同:
(function($){
$('#thumbcarousel').carousel(0);
var $thumbItems = $('#thumbcarousel .item');
var $items = $('#carousel .item');
var numItems = $items.length;
var numThumbItems = $thumbItems.length;
var thumbGroup = Math.ceil(numItems/numThumbItems);
$('#carousel').on('slide.bs.carousel', function (event) {
var curThumbIndex = $thumbItems.index($thumbItems.filter('.active').get(0));
var activeSlideNum = $items.index(event.relatedTarget)+1;
var thumbIndex = (Math.ceil(activeSlideNum/thumbGroup))-1;
if (curThumbIndex>thumbIndex) {
$('#thumbcarousel').one('slid.bs.carousel', function (event) {
$('#thumbcarousel').carousel(thumbIndex);
});
if (curThumbIndex === ($thumbItems.length-1)) {
$('#thumbcarousel').carousel('next');
} else {
$('#thumbcarousel').carousel(numThumbItems-1);
}
} else {
$('#thumbcarousel').carousel(thumbIndex);
}
});
})(jQuery);
同步选项 2:
我还更新了 Bootply 演示,以稍微不同的方式完成此操作,但这种方法需要额外的标记。在 Bootply 演示中,您将看到可以在每个主要轮播项目上使用 data-thumb 设置拇指项目目标。该数字表示拇指轮播中目标项目的零基索引。例如:
<div id="carousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="item active" data-thumb="0">
<img src="http://placehold.it/350x250/e8117f/fff&text=Product+Main">
</div>
<div class="item" data-thumb="0">
<img src="http://placehold.it/350x250/00ffff/000&text=Product+Image+2">
</div>
<div class="item" data-thumb="0">
<img src="http://placehold.it/350x250/ff00ff/fff&text=Product+Image+3">
</div>
<div class="item" data-thumb="0">
<img src="http://placehold.it/350x250/ffff00/000&text=Product+Image+4">
</div>
<div class="item" data-thumb="1">
<img src="http://placehold.it/350x250/7B1C8E/fff&text=Product+Image+5">
</div>
<div class="item" data-thumb="1">
<img src="http://placehold.it/350x250/9EF383/000&text=Product+Image+6">
</div>
<div class="item" data-thumb="1">
<img src="http://placehold.it/350x250/D64908/fff&text=Product+Image+7">
</div>
<div class="item" data-thumb="1">
<img src="http://placehold.it/350x250/E3A3A1/000&text=Product+Image+8">
</div>
</div>
</div>
那么你可以改用这个脚本:
(function($){
$('#thumbcarousel').carousel(0);
var $thumbItems = $('#thumbcarousel .item');
$('#carousel').on('slide.bs.carousel', function (event) {
var $slide = $(event.relatedTarget);
var thumbIndex = $slide.data('thumb');
var curThumbIndex = $thumbItems.index($thumbItems.filter('.active').get(0));
if (curThumbIndex>thumbIndex) {
$('#thumbcarousel').one('slid.bs.carousel', function (event) {
$('#thumbcarousel').carousel(thumbIndex);
});
if (curThumbIndex === ($thumbItems.length-1)) {
$('#thumbcarousel').carousel('next');
} else {
$('#thumbcarousel').carousel(numThumbItems-1);
}
} else {
$('#thumbcarousel').carousel(thumbIndex);
}
});
})(jQuery);