jQuery Cycle 2 Carousel using pager and next / prev links

Doidgey picture Doidgey · Oct 15, 2013 · Viewed 9.4k times · Source

I've been playing around with Cycle 2 plugin, in particular this example: http://jquery.malsup.com/cycle2/demo/caro-pager.php

I've got this locally, and i want to change it so that i have the option if i need to, to have number pagers so i could have both next / prev and the pager, or just hide one and use the other.

The problem is if you look at the example I've setup; http://jsfiddle.net/ZYEbT/ - You will be able to see that the pager does not appear properly due to the data-allow-wrap="false" property on the carousel, but removing this breaks the clickable carousel images, but shows all the of pager links.

Is it possible to have both next and prev working alongside the pager?

My HTML:

<div id="slideshow-1">
    <div id="cycle-1" class="cycle-slideshow"
        data-cycle-slides="> div"
        data-cycle-timeout="0"
        data-cycle-caption="#slideshow-1 .custom-caption"
        data-cycle-fx="fade"
        >
        <div><img src="http://jquery.malsup.com/cycle2/images/beach1.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach2.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach3.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach4.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach5.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach6.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach7.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach8.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach9.jpg"></div>
    </div>
</div>

<div id="carousel">
    <div id="cycle-2" class="cycle-slideshow"
        data-cycle-slides="> div"
        data-cycle-timeout="0"
        data-cycle-prev="#carousel .cycle-prev"
        data-cycle-next="#carousel .cycle-next"
        data-cycle-fx="carousel"
        data-allow-wrap="false"
        data-cycle-pager="#custom-pager"
        data-cycle-pager-template="<span><a href=#> {{slideNum}} </a></span>"
        >
        <div><img src="http://jquery.malsup.com/cycle2/images/beach1.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach2.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach3.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach4.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach5.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach6.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach7.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach8.jpg"></div>
        <div><img src="http://jquery.malsup.com/cycle2/images/beach9.jpg"></div>
    </div>


    <a href="#" class="cycle-prev">&laquo; prev</a>
    <a href="#" class="cycle-next">next &raquo;</a>

</div>

<div id="custom-pager" class="center"></div>

jQuery:

jQuery(document).ready(function($){
        var slideshows = $('.cycle-slideshow').on('cycle-next cycle-prev', function(e, opts) {
            // advance the other slideshow
            slideshows.not(this).cycle('goto', opts.currSlide);
        });

        $('#cycle-2 .cycle-slide').click(function(){
            var index = $('#cycle-2').data('cycle.API').getSlideIndex(this);
            slideshows.cycle('goto', index);
        });
    });

If i am missing anything, please let me know what you need to see.

Answer

Bryan picture Bryan · Oct 16, 2013

I think I have it figured out, let me know if I am missing something.

First off, you should have your pager be in your main slideshow. Then in addition to using the cycle-next cycle-prev events, add this. It will run when the pager is activated, and move both slideshows to the correct slide.

var slideshows2 = $('.cycle-slideshow').on('cycle-pager-activated', function(e, opts) {
    // advance the other slideshow
    slideshows2.not(this).cycle('goto', opts.currSlide);
});

Here is an updated fiddle that shows it in action: Fiddle