Ahmed Nuaman


builder of internets ~ developer of dreams ~ tamer of Dachshunds

Quite Possibly the Easiest (and Smallest?) jQuery Carousel

Here’s a quickie: a nice and small jQuery carousel…

Take some list items:

<div id="carousel">
<ul>
    <li>Hello!</li>
    <li>Why hello again?</li>
    <li>We need to stop meeting like this...</li>
</ul>
</div>

And apply some CSS:

ul,
li
{
    margin: 0;
    padding: 0;
}
#carousel
{
    display: block;
    position: relative;
    width: 400px;
    height: 250px;
    overflow: hidden;
}
#carousel ul
{
    display: block;
    width: 5000px;
    height: 250px;
}
#carousel li
{
    display: block;
    float: left;
    width: 400px;
    height: 250px;
    background: #0080FF;
    color: #FFFFFF;
    font-size: 12pt;
}

And finally sprinkle with some JS:

$( document ).ready( function()
{
    var t   = $( '#carousel' );
    var m   = $( 'li', t ).length;
    var w   = $( 'li:first', t ).outerWidth();
    var i   = 0;

    setInterval( function()
    {
        if ( i >= m )
        {
            i   = 0;
        }

        $( 'ul', t ).animate({
            'margin-left'   : i * w * -1 + 'px'
        }, 1000, 'easeInOutQuint' );

        i++;
    }, 3000 );
});

And voila:

Where have comments gone?

Good question my old fruity. I'm now sticking any post discussions on Google+. Why? Well simply it's better. WordPress's comment system isn't very elegant and nor are ones like Disqus or Livefyre, so to save hassle I've just shipped them off to a social network, like a real boss.

Search

My social skills

Latest blog posts

  • Loading posts...

Subscribe in a reader

Latest tweets

  • Loading tweets...