From our sponsor: Ready to show your plugin skills? Enter the Penpot Plugins Contest (Nov 15-Dec 15) to win cash prizes!
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
- Demo 1: A minimum of 3 (default) images are always visible.
- Demo 2: Vertical with a minimum of 3 items visible.
- Demo 3: Fixed wrapper with a minimum of 2 images is always visible.
- Demo 4: Usage in an image gallery; a minimum of 4 images are visible.
I hope you like this plugin and find it useful!
definitely useful mary lou.. always you are inspiring me.. good luck..
I use this site http://assadotcom.blogspot.com/2010/08/viewlike.html to see the demo and the result was 100% working.
You Rock Mary!!!
I Love U =¤
Thank you. Elastislider is a nice name. Love these adaptive-responsive stuff, keep them coming!
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!
been looking for something simple and functional just like this. as always, you are MY HERO!
Thanks for this very useful plugin.Very unique Carousel plugin!!
Does this only support images or can it handle other content as well?
Very creative and useful. Does it work with vertical?
It’d be nice if it had a vertical property as well. Plus methods to advance to next and previous through a custom element.
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.
100% working.. thank mary lou
What Alex said! 😉
Here’s one more site http://assadotcom.blogspot.com/2011/09/responsive-design-testing.html to see a demo of this paper.
Amazing!
mary lou is back…\m/
i am using this in my new project. thnk you so much.
I have downloaded and customized it with my style it is really working…
Thanks to share.
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.
Can we add any description or explanation to the slider?
byw thanx you so much..
Nice job and great update in your twitter!
Quick follow-up on my post. Upgraded to jQuery v1.6.1 and now the carousel loads as expected.
Great work! Thanks
I like how the carroussel adapts on resizing!
Can somebody tell me how to make item a href working well?
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.
Great work! Thanks
would it be possible to use items with different widths? i am desperately looking for a solution for this…
thx,
Jürgen
Thanks so much for this…simply excellent!
adding vertical scrolling option would be nice
Does it possible to make Auto-Scroll option ??
great, thanks! 😀
is there a way to add pagination to the slider, which highlights the current item?
m
Is there a way to auto-scroll the slider on page load? Please! Thanks
Make please work items with different widths.
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
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) );
});
thank you very much . . .
@Ives Thanks! Saved me a lot of work there! Script functions now as a thumbnail viewer! Excellent!
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.
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’
Is putting a character into the following of a picture, etc. possible?
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);
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
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!
@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?
this site are great!! thanks Mary, thanks Ives
but i have a problem, if i set imageW+margin more then es-carousel-wrapper, that stop working
how can I replace the onClick on mouseover function?
@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… 😀
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.
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.
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.