简体   繁体   English

具有分页功能的自举轮播-使用轮播控件时,当前幻灯片在导航中不活动

[英]Bootstrap carousel with pagination - current slide not active in navigation when using carousel control

I made a carousel that uses pagination as navigation instead of the dots. 我制作了一个使用分页而不是圆点的导航轮播。

When you navigate using the page numbers, it works fine and the active class is added. 使用页码导航时,它可以正常工作并添加了活动类。 When you navigate using the controls (left and right), it goes to the next/prev page correctly, but the active class in the navigation doesn't change. 当您使用控件(向左和向右)导航时,它将正确地转到下一页/上一页,但是导航中的活动类不会更改。

The page in question is here 有问题的页面在这里

It's from this jsfiddle using the code below: http://jsfiddle.net/juddlyon/Q2TYv/10/ 它来自使用以下代码的jsfiddle: http : //jsfiddle.net/juddlyon/Q2TYv/10/

As far as I can tell, I followed the example from the jsfiddle. 据我所知,我遵循了jsfiddle中的示例。 What went wrong? 什么地方出了错?

// invoke the carousel
$('#myCarousel').carousel({
interval: false
});

/* SLIDE ON CLICK */ 

$('.carousel-linked-nav > li > a').click(function() {

// grab href, remove pound sign, convert to number
var item = Number($(this).attr('href').substring(1));

// slide to number -1 (account for zero indexing)
$('#myCarousel').carousel(item - 1);

// remove current active class
$('.carousel-linked-nav .active').removeClass('active');

// add active class to just clicked on item
$(this).parent().addClass('active');

// don't follow the link
return false;
});

/* AUTOPLAY NAV HIGHLIGHT */

// bind 'slid' function
$('#myCarousel').bind('slid', function() {

// remove active class
$('.carousel-linked-nav .active').removeClass('active');

// get index of currently active item
var idx = $('#myCarousel .item.active').index();

// select currently active item and add active class
$('.carousel-linked-nav li:eq(' + idx + ')').addClass('active');

});

You are using a newer version of bootstrap then in the example used. 您正在使用的示例中使用的是更高版本的引导程序。 The event slid is renamed to slid.bs.carousel . 本次活动slid被重命名为slid.bs.carousel

So binding to the new event will work for you. 因此,绑定到新事件将为您工作。

$('#myCarousel').bind('slid.bs.carousel', function() {

    // remove active class
    $('.carousel-linked-nav .active').removeClass('active');

    // get index of currently active item
    var idx = $('#myCarousel .item.active').index();

    // select currently active item and add active class
    $('.carousel-linked-nav li:eq(' + idx + ')').addClass('active');

});

The corresponding link to the bootstrap docs: http://getbootstrap.com/javascript/#carousel-events 引导文档的相应链接: http : //getbootstrap.com/javascript/#carousel-events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM