开发者

Simple JQuery Fade ticker

I've looked at multiple tickers and they are all far to weighted. I'm after a very simple fadeIn() fadeOut() JQuery ticker for a list of elements to display titles.

<li>Story 1</li>
<li>Story 2</li>
<li>Story 3</li>
<li>Story 4</li>
<li>Story 5</li>

I looked at the next function but I don't know how to make it show the elements I want. So I'm after something very simple. All it needs is an interv开发者_JS百科al a fade out and a fade in on a loop.


I did a very simple fader, very light weight. used it for images but changed it for divs:

Script

var aniSpd01 = 1000;
var fadeSpd01 = 1000;

$(function() {
    var startIndex = 0;
    var endIndex = $('#aniHolder div').length;
    $('#aniHolder div:first').fadeIn(fadeSpd01);

    window.setInterval(function() {
    $('#aniHolder div:eq(' + startIndex + ')').delay(fadeSpd01).fadeOut(fadeSpd01);
        startIndex++;
        $('#aniHolder div:eq(' + startIndex + ')').fadeIn(fadeSpd01);

        if (endIndex == startIndex) startIndex = 0;
    }, aniSpd01);
});

HTML

<div id="aniHolder">
    <div>Story 1</div>
    <div>Story 2</div>
    <div>Story 3</div>
</div>

CSS

#aniHolder {width:640px; height:480px; }
#aniHolder div {position:absolute; width:640px; height:480px; display:none;}


Try the jquery Cycle plugin; I've used it for creating a simple set of rotating text links, there's a "lite-min" version which keeps it nice and lean if you only want basic functionality and effects.


Untested. This simply reschedules the fade out/in, which are chained together, on the next element after the current one completes.

function fadeTicker( $collection, $elem, interval )
{
     var $next = $collection.eq(($collection.index($elem) + 1) % $collection.length));
     $(elem).fadeOut( function() {
          $(next).fadeIn( function() {
               setTimeout( function() {
                       fadeTicker( $collection, $next, interval );
                   },
                   interval
               );
          });
     );
);

Used as:

$(function() {
    fadeTicker( $('li'), $('li:first'), 5000 );
}


The jQuery WebTicker is also a very good alternative for rotating text links; whilst giving you complete control of the direction of rotation and the speed; it also allows flexibility of colours and size using CSS. The ticker allows non-stopping rotation and once you reach the last item the first one will start straight away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜