Elastislide – A Responsive jQuery Carousel Plugin

Elastislide is a responsive image carousel that will adapt fluidly in a layout. It is a jQuery plugin that can be laid out horizontally or vertically with a pre-defined minimum number of shown images.

With the responsive awakening in web design it becomes important to not only take care of the visual part of a website but also of the functionality. Elastislide is a responsive jQuery image carousel that will adapt its size and its behavior in order to work on any screen size. Inserting the carousel’s structure into a container with a fluid width will also make the carousel fluid.

In a carousel, one could think that simply making its container “shorter” will solve the problem on smaller screens, but in some cases (e.g. when we have bigger images) it might be reasonable to resize the items as well. This and other options are part of Elastislide’s properties.

Elastislide uses the jQuery++ for the swipe events on touch devices.

Tiny break: 📬 Want to stay up to date with frontend and trends in web design? Subscribe and get our Collective newsletter twice a tweek.

The beautiful images are by talented Sherman Geronimo-Tan and they are licensed under Creative Commons Attribution 2.0 Generic (CC BY 2.0).

The HTML Structure

When using the plugin, you’ll simply have to apply it to an unordered list with images (optionally with anchors). Make sure the surrounding container is fluid, i.e. has a percentage-based width. (If you put the carousel in a container with a fixed width, it will not adapt. So make sure that your container has a fluid layout.)

<ul id="carousel" class="elastislide-list">
	<li><a href="#"><img src="images/1.jpg" alt="image01" /></a></li>
	<li><a href="#"><img src="images/2.jpg" alt="image02" /></a></li>
	<li><a href="#"><img src="images/3.jpg" alt="image03" /></a></li>
	<!-- ... -->
</ul>

Use your preferred ID and call the plugin like this:

$('#carousel').elastislide();

Options

Elastislide has the following default options:

$.Elastislide.defaults = {
	// orientation 'horizontal' || 'vertical'
	orientation : 'horizontal',

	// sliding speed
	speed : 500,

	// sliding easing
	easing : 'ease-in-out',

	// the minimum number of items to show. 
	// when we resize the window, this will make sure minItems are always shown 
	// (unless of course minItems is higher than the total number of elements)
	minItems : 3,

	// index of the current item (left most item of the carousel)
	start : 0,
	
	// click item callback
	onClick : function( el, position, evt ) { return false; },
	onReady : function() { return false; },
	onBeforeSlide : function() { return false; },
	onAfterSlide : function() { return false; }
};

It is also possible to dynamically add new items to the carousel. The following is an example on how to achieve that:


var carousel = $('#carousel').elastislide();
...

$('#carousel').append("<li><a href="#"><img src="images/10.jpg" alt="image03" /></a></li>");
carousel.add();

Note that if you use the Elastislide for a vertical layout, the height of the container will be automatically set according to how many images you choose to be visible.

This works down to IE8, but if you’d like to use it in IE8 you’ll need to add comments in your list in order to prevent white spaces.

Demos

I hope you like this plugin and find it useful!

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay in the loop: Get your dose of frontend twice a week

Fresh news, inspo, code demos, and UI animations—zero fluff, all quality. Make your Mondays and Thursdays creative!

Feedback 135

Comments are closed.
  1. Is there a way to make this more like a news slider, with one image+description coming up at a time? I’m working on a site that requires one like that and having it work on mobile would be fabulous!

  2. It’d be nice if it had a vertical property as well. Plus methods to advance to next and previous through a custom element.

  3. Thank you for this. Is there any way to force a redisplay? I had to $(‘#carousel’).append(”).elastislide( ‘add’, ” ); to force it because my carousel wasn’t getting properly resized onload, only after a resize.

  4. I have downloaded and customized it with my style it is really working…

    Thanks to share.

  5. Miguel, were you able to find a solution? I’m running into the same problem where the carousel isn’t loading properly until you resize the browser.

  6. I, too, am also wondering if there is a way to make the images into links. I thought I could use this for a thumbnail viewer, but it doesn’t seem to be working. 🙁

    Clicking on the thumbnail doesn’t take me to the larger size image I link it to.

  7. would it be possible to use items with different widths? i am desperately looking for a solution for this…

    thx,
    Jürgen

  8. great, thanks! 😀

    is there a way to add pagination to the slider, which highlights the current item?

    m

  9. Super awesome, thanks so much for your hard work on this. Is there a way we can specify how many items to scroll when you click next/previous? I’d like to only scroll 1 image at a time

  10. To fix the problem that links are not working, comment or remove the “return false”; from the line 268.

    FROM:

    // item click event
    this.$items.bind(‘click.elastislide’, function( event ) {
    instance.options.onClick( $(this) );
    return false;
    });

    TO

    // item click event
    this.$items.bind(‘click.elastislide’, function( event ) {
    instance.options.onClick( $(this) );
    });

  11. @Ives Thanks! Saved me a lot of work there! Script functions now as a thumbnail viewer! Excellent!

  12. I’m not sure if this will work for anyone else but I needed a small change for my purposes. So here it is. I needed a long list of images alphabatized with easy access from the keyboard instead of scrolling. You can insert this right after _initEvents around line 225:

    ….

    _initEvents : function() {

    var instance = this;

    // key support
    $(document).keypress(function(e) {
    var code = String.fromCharCode(e.charCode)
    // get the items with the data-key matching the key pressed
    item = $(‘*[data-key=’+code+’]’);
    // get the index of our first match
    idx = item.parent().children().index(item);
    // make sure we have a match then set as current
    if(idx >= 0) {
    instance.current = idx;
    // set values again
    instance._setCurrentValues();
    // slide to the current element
    clearTimeout( instance.resetTimeout );
    instance.resetTimeout = setTimeout(function() {
    instance._slideToCurrent();
    }, 200);
    }
    });

    ….

    Now in your ul give each li a data-key attribute.



    ….

    With this you will be able to hit a key and automatically scroll to the first image starting with that letter. Of course it may need tweeked a bit for others like right now I just have it watching document for keypresses because I have no other inputs on my page. But it shouldn’t be difficult to change for other needs.

    I hope this helps someone. As I have learned quite a bit and have gotten so many ideas from this site.

  13. Hmm… not sure why this didn’t show up in my previous comment but for your list items just include a custom attribute for example:

    data-key=’a’
    data-key=’b’

  14. In example 3, the last item showing in the carousel is the first item when the user advances. My suggested fix is changing line 195 from:

    this.fitCount = Math.floor(this.visibleWidth / this.itemW);

    to:

    this.fitCount = Math.round(this.visibleWidth / this.itemW);

  15. If you, like me, want to link the images with Shadowbox, you can simple put something like this:

    $(‘.es-carousel-wrapper’).elastislide({
    imageW : 200,
    onClick : function( $item ) {
    Shadowbox.open();
    }
    });

    Don’t forget to initialize shadowbox before the call ;D

  16. This is almost perfect for my current project. Only problem I’m having is how to adjust the width of the elements. Something (in script?) is setting them at 80px and I need control over this.

    Thanks!

  17. @BRUNO CANTUÁRIA i tried to link images with shadowbox. it work on normal pictures but gallery photos. can you explain a little bit more?

  18. @Miguel and Ryan:
    On line 229 in the class, you can see that the window.resize event is being bound to a lambda function that causes reinitialization to occur.
    For those of us loading the content we wish to carousel, and the carousel script itself, and the css, and whatnot else with javascript via an AJAX request, it might be useful to change the event that function binds to.
    In my case, I just did a hack job because I literally had 10 minutes to implement this code; I just duplicated the function, bound the window.mouseover event to it, and set up a class variable that tells my duplicate function not to reinitialize when I mouse over any other time after the first. A resize resets the class variable.
    Hey, it works… 😀

  19. Thanks, Mary Lou.
    This is perfect developed stuff, works so precise.

    How to slide not fixed width images?

    I would love to give other width for one or two items, but after specify the ID to them and setting their width more than it is in imageW (295px) parameter,

    $(document).ready(function() {
    $(“#ID”).css(“width”,”590px”);
    });

    there was no space in container for all items.

    So I have added 295px to sliderW parameter (line 168):

    width : this.sliderW + 295

    and now no items goes down but there is no posibility to see the whole last item – it stays hidden by browser window.

    Do you have any idea how to solve it?

    However I only want to slide not fixed width images…

    Thx,
    .m.

  20. Like always on Codrops, great stuff! When trying to implement the code into a mobile website there seems to be a conflict with the jquery.mobile-1.0a3.min.css. The code doesn’t work with this css in place, when I remove the css, all works fine, but I still need the mobile.css functionality.
    Any thoughts, suggestions? Please look into this.

    Greetings from the Netherlands, Koen.

  21. Never mind the previous post, I’ve solved it.
    Am I right in thinking that I can’t use multiple instances of the same code on a single website? My “first” set of code works like a charm, I can’t get a second and/or third set to work properly. It keeps putting the images outside the carousel, stacked vertically. I’m using the same div’s, same CSS etc. Is this a javascript issue? Please help!

    Any thoughts?

    Greetings from the Netherlands, Koen.