1

试图让这个自定义寻呼机为 jQuery Cycle 工作。

我希望每 5 张图像pagerAnchorBuilder返回一个list项目,以便稍后我可以将寻呼机变成它自己的循环。

这是代码:

  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>';

            }
    }

所以最后我希望 Cycle 返回这个:

<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>

请忽略setSlide(#)JS 中返回的 HTML 字符串中的 和 类值,我可以控制它。即使经过2个小时的搜索,我也无法弄清楚。

提前致谢!

4

1 回答 1

3

我最终弄清楚了。这是修改后的代码,为方便起见注释:

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);
        }
        });

}
于 2011-12-01T19:30:04.150 回答