Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger next page owl carousel 2

I'm using Owl Carousel 2 and I have custom nav buttons that I intend to use for next/prev item and next/prev page. I'm using the following to trigger the next item:

var slider = $('.owl-carousel');

$('#nextItem').click(function () {
    slider.trigger('next.owl.carousel');
});

That works fine, but I also need to trigger the next page (similar to how the dots work). It looks like there is a slideBy option that you can use to slide by a page, but this won't help since I need both item and page navigation.

$('#nextPage').click(function () {
    // go to next page
});
like image 963
tqrecords Avatar asked Jun 13 '15 22:06

tqrecords


3 Answers

You could trigger clicks on the dots:

// Go to page 3
$('.owl-dot:nth(2)').trigger('click');

To go to the next page or to the first if you're on the last, you can do it like this:

var $dots = $('.owl-dot');

function goToNextCarouselPage() {    
    var $next = $dots.filter('.active').next();

    if (!$next.length)
        $next = $dots.first();

    $next.trigger('click');
}

$('.something-else').click(function () {
    goToNextCarouselPage();
});
like image 77
Rudi Avatar answered Nov 09 '22 08:11

Rudi


You could trigger clicks to next page (slider) only add option

slideBy: 3 
.....         
responsive:{
                0:{
                    items:1,
                    nav:false
                },
                600:{
                    slideBy: 3,
                    items:3,
                    nav:true
                },
                1000:{
                    slideBy: 3, //Option for trigger next page to click on nav.
                    items:3,
                    nav:true,
                    loop:true
                }
            }
.....

like image 21
Webfer Avatar answered Nov 09 '22 06:11

Webfer


$('#js-carousel-models').owlCarousel({
    center: true,
    items: 1.5,
    loop:true,
    margin: 0,
    dots: true,
    autoplay: true
});

var owl = $('#js-carousel-models');
owl.owlCarousel();

$('.owl-item').click(function() {        
    if( $(this).next().hasClass('center') ){
        // scroll to prev
        owl.trigger('prev.owl.carousel');
    }
    if( $(this).prev().hasClass('center') ){
        // scroll to next
        owl.trigger('next.owl.carousel');
    }            
})
like image 20
Oleg Avatar answered Nov 09 '22 08:11

Oleg