1

I'm using the jQuery cycle plugin to cycle through multiple divs in a container. All is working well the only functionality that I would like to add that I can't seem to find an option for with in the plugin is the ability to hide both buttons if there is only one slide present with in the container. My current code is this:

    //Goal summary page cycler
    $('#showGoal').after('<div id="nav">')
                .cycle({
                    fx: 'scrollHorz',
                    speed:300,
                    prev: '#goalPrev',
                    next: '#goalNext',
                    timeout: 0,
                    pager: '#nav',                        
                    after: onAfter, 
                    pagerAnchorBuilder: function(idx, slide) {
                        return'<a href="#"></a>';
                    }
                });

            // call back to remove buttons on first and last slide / adjust height of container
            function onAfter(curr, next, opts, fwd) {
                var index = opts.currSlide;
                $('#goalPrev')[index == 0 ? 'hide' : 'show']();
                $('#goalNext')[index == opts.slideCount - 1 ? 'hide' : 'show']();

                //get the height of the current slide
                var $ht = $(this).height();
                //animates the container's height to that of the current slide 
                $(this).parent().animate({ height: $ht });
            }

I'm guessing I can either check the index and remove them both that way or possibly do something along the lines of an if statement for example (pseudo code) :

    var index = this.index();
    var numOfSlides = 0;

    if ( numOfSlide < index ) {
       //hide the buttons 
   }
   else
   {
       //show the buttons
   }

Thanks for any possible help with this!

4

3 に答える 3

1

問題が解決し、それを見逃すのは馬鹿げているように感じます。最初は矢印の表示をなしに設定する必要があります。

于 2012-07-06T18:34:25.120 に答える
1

すでに機能しているはずではありませんか?スライドが 1 つしかない場合は、onAfter 関数で処理する必要があります。1 つのスライドでは、インデックスは常に 0 です。また、1 スライドから 1 を引いたものになるため、opts.slideCount - 1 もゼロになります。

編集:

ただし、以下で説明するように、1 つのスライドを処理するときに関数が呼び出されない可能性があるため、ページの読み込み後に onAfter を呼び出す必要がある場合があります。

于 2012-07-05T22:37:18.340 に答える