如何在Owl Carousel 2的可见项目中的第一个和最后一个项目中添加课程?

She*_*r R 2 html css jquery css3 owl-carousel

我需要在Owl Carousel 2上的当前可见项目的第一个和最后一个项目中添加一个类.

Mar*_*son 6

DEMO:

http://plnkr.co/edit/t9URfKq9Mwh9jO705h7u?p=preview

猫头鹰轮播为所有可见项添加了一个活动类.因此,您可以看到我已经编写了一个脚本来循环遍历所有活动类的owl-item,然后使用第0个和最后一个索引,我添加了两个不同的类.

使用项目中的代码,您将获得添加的类.

jQuery(document).ready(function($) {

    var carousel = $(".latest-work-carousel");
    carousel.owlCarousel({
        loop : true,
        items : 3,
        margin:0,
        nav : true,
        dots : false,
    });

    checkClasses();
    carousel.on('translated.owl.carousel', function(event) {
        checkClasses();
    });

    function checkClasses(){
        var total = $('.latest-work-carousel .owl-stage .owl-item.active').length;

        $('.latest-work-carousel .owl-stage .owl-item').removeClass('firstActiveItem lastActiveItem');

        $('.latest-work-carousel .owl-stage .owl-item.active').each(function(index){
            if (index === 0) {
                // this is the first one
                $(this).addClass('firstActiveItem');
            }
            if (index === total - 1 && total>1) {
                // this is the last one
                $(this).addClass('lastActiveItem');
            }
        });
    }


});
Run Code Online (Sandbox Code Playgroud)

DEMO:

http://plnkr.co/edit/t9URfKq9Mwh9jO705h7u?p=preview