Search code examples
javascriptjqueryowl-carousel

Owl Carohausel onChange triggering only on page change


I am using Owl Carousel 2 with 3 items per page setup. I want to select every second item when the slide event occurs because I am blurring first and second items and making the only second one visible.

I am using this piece of jQuery code to achieve this:

$("#service-slider .active:eq(1)").addClass("myActive");

And this is how i initiate my Owl Carousel:

$("#service-slider").owlCarousel({
    loop: true,
    margin: 10,
    nav: true,
    dots: false,
    autoplay: true,
    autoplayTimeout: 5000,
    autoplayHoverPause: false,
    smartSpeed: 1500,
    onChange: activeSelect(),
    onDrag: activeSelect(),
    onTranslate: activeSelect(),
    responsive: {
        0: {
            items: 1
        },
        600: {
            items: 1
        },
        1000: {
            items: 3
        }
    }
});

$("#service-slider .active:eq(1)").addClass("myActive");

function activeSelect() {
    $("#service-slider .active").removeClass("myActive");
    $("#service-slider .active:eq(1)").addClass("myActive");
}

var owl = $('#service-slider');
owl.owlCarousel();
owl.on('next.owl.carousel', function (event) {
    $("#service-slider .active").removeClass("myActive");
    $("#service-slider .active:eq(1)").addClass("myActive");
});
owl.on('prev.owl.carousel', function (event) {
    $("#service-slider .active").removeClass("myActive");
    $("#service-slider .active:eq(1)").addClass("myActive");
});

It is working fine for the first time but when an item transitioned it is not working; Only working when the whole page changes.

Here is a fiddle: https://jsfiddle.net/iCromwell/mpoxf9rc/1/


Solution

  • user7290573 found a solution. You can use the center option of the Owl Carousel to achieve what I want. His fiddle can be found here: https://jsfiddle.net/y2xhr4dk/

    $("#service-slider").owlCarousel({
        loop: true,
        margin: 10,
        nav: true,
        dots: false,
        center: true,
        autoplay: true,
        autoplayTimeout: 5000,
        autoplayHoverPause: false,
        smartSpeed: 1500,
        responsive: {
            0: {
                items: 3
            },
            600: {
                items: 3
            },
            1000: {
                items: 3
            }
        }
    });