Search code examples
javascriptjqueryoperatorscyclemodulus

custom pager return for jquery cycle using Modulus


Trying to get this custom pager to work for jQuery Cycle.

I want the pagerAnchorBuilder to return a list item every 5 images so later on I can turn the pager into it's own cycle.

Here's the code:

  pagerAnchorBuilder: function(idx,slide){
        var $slideCount = $('#ul-homecycle > li').length; 
            if ((idx==0) || (idx%5 === 0)){
                return '<li><a href="javascript:setSlide('+ idx +')" class="transhover"><img src="../images/home/thumb_carousel' + idx + '.jpg" width="183" height="72" /></a></li>';
            }
            else{ 
                return '<a href="javascript:setSlide('+ idx +')" class="transhover"><img src="../images/home/thumb_carousel' + idx + '.jpg" width="183" height="72" /></a>';

            }
    }

So in the end I want Cycle to return this:

<ul>
<li>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
</li>
<li>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
<a href="javascript:void(0);">derp</a>
</li>
</ul>

Please disregard the setSlide(#) and the class values from the HTML string return in the JS, I have that under control. I can't figure this out even after 2 hours of searching.

Thanks in advance!


Solution

  • I ended up figuring it out. Here's the modified code, commented for your convenience:

    function cycles(){
        //Activate main slide with proper pager/carousel container.
        $('#ul-homecycle').after('<div id="homecycle-carousel-container"><a href="javascript:void(0);" id="btn-homecycle-prev" class="transhover"></a><a href="javascript:void(0);" id="btn-homecycle-next" class="transhover"></a><ul id="homecycle-carousel">').cycle({
            timeout: 7000,
            speed: 500,
            sync: true,
            fx: 'fade',
            fit: 0,
            startingSlide: 2,
            activePagerClass:'active',
            slideResize: 0,
            pager:  '#homecycle-carousel',
            pagerAnchorBuilder: function(idx,slide){
                return '<a href="javascript:setSlide('+ idx +')" class="transhover"><img src="../images/home/thumb_carousel' + idx + '.jpg" width="183" height="72" /></a>';
            },
            // Build pager that automagically hightlights updated pager thumb and also continue to next pager slide if the next thumb isn't visible
            updateActivePagerLink: function(pager, activeIndex) { 
                $(pager).find('a').not(':eq('+activeIndex+')').removeClass('active').children('img').fadeTo(200,.5)
                $(pager).find('a:eq('+activeIndex+')').addClass('active').children('img').fadeTo(200, 1); 
    
                //If next slide in carousel is hidden then go to next carousel slide
                if ($(pager).find('a:eq('+activeIndex+')').parent('li').css('display') == "none"){
                    $('#homecycle-carousel').cycle('next');
                }
            }
        });
            //Before building the carousel wrap an Li around every 5 thumbs    
            $('#homecycle-carousel > a').each(function(i) {
               var a = $('#homecycle-carousel > a');
                for( var i = 0; i < a.length; i+=5 ) {
                    a.slice(i, i+5).wrapAll('<li></li>');
                }
            });
            //Now that we have Lis wrapped around every 5 thumbs, initiate new cycle    
            $('#homecycle-carousel').cycle({
                easing: 'easeOutSine',
                prev: '#btn-homecycle-prev',
                next: '#btn-homecycle-next',
                slideExpr: 'li',
                activePagerClass:'active',
                timeout:0,
                speed: 500,
                fx: 'scrollHorz'
            });
            //Artifically activate new thumb 
            $('#homecycle-carousel li a').click(function(){
            if(!$(this).hasClass('active')){
                $('#homecycle-carousel li a').not(this).removeClass('active').children('img').fadeTo(200,.5);
                $(this).addClass('active').children('img').fadeTo(200,1);
            }
            });
    
    }