如何跳转到 Bootstrap 轮播中的特定项目?



使用Bootstrap的轮播,我希望能够点击链接并跳转到轮播中的特定幻灯片。我该怎么做?我必须添加什么jquery?

这是我的html(它不起作用,但可以让您了解我想做什么):

<a href="#panel">Panel</a>
<div id="myCarousel" class="carousel slide">
 <div class="carousel-inner">
  <div id="panel" class="item">
...
  </div>
 </div>
</div>

如果您的链接与面板的顺序相同,您可以执行以下操作:

$('.link').on('click', function() {
    $('#carousel').carousel($(this).index());
});

如果您只有一个链接,只需对其进行硬编码:

var i = 2; //index of the panel    
$('#link').on('click', function() {
    $('#carousel').carousel(i);
});

从文档中:

Methods:
carousel(options)
Initializes the carousel with an optional options object and starts cycling through items.
$('.carousel').carousel({
  interval: 2000
})
.carousel('cycle')
Cycles through the carousel items from left to right.
.carousel('pause')
Stops the carousel from cycling through items.
.carousel(number)
Cycles the carousel to a particular frame (0 based, similar to an array).
.carousel('prev')
Cycles to the previous item.
.carousel('next')
Cycles to the next item.

对我来说,指定的脚本化方法不起作用,但是基于另一个示例,我能够使用数据标签使其工作

<a data-target="#myCarousel" data-slide-to="0" href="#">First</a>

请注意,data-slide-to索引基于 0,这是在 Bootstrap 4 上测试的

最新更新