Many projects need a carousel of some sort. As most libraries or plugins are big and not so easy to extend I did what any respectable developer would do. I reinvented the wheel and created my own Carousel.
The Carousel is a plain and simple slider. It slides a ul behind an element which has overflow: hidden. Get the code on Github. The cloned code contains a simple example.
How to use
A very simple setup could look like this:
$(function()
{
var carouselOpts =
{
prev: '.carousel-prev',
next: '.carousel-next',
loop: false,
auto: false,
startAt: 2,
speed: 1000
},
carouselInstance = new Carousel.Base($('.carousel'), carouselOpts),
carouselPager = new Carousel.Pager($('.carousel-pager'));
// Or as a jQuery plugin
$('.carousel').carousel(carouselOpts);
$('.carousel-pager').carouselPager();
});
Options
For the Carousel.Base class:
speedSpeed in ms.timeoutSpeedSpeed in ms, is only applied if auto is set to true.easingDefault iseaseInOutExpo.autoEnables autorun.loopIf set to true the carousel will go to the first slide when clicking the next button after the last slide was shown.prevjQuery selector.nextjQuery selector.startAtStart at a specific slidestepuse this value to override the with of the slides. Defaults the CSS with of the slides.
For the Carousel.Pager class:
activateOnActivate the pager item after or before the carousel has moved. Defaults to:beforemove. Also available:aftermove.killAutoRunAfterPagerIsUsedIf the carousel’s auto option has been set, interacting with the carousel will kill the autorun.carouselToMovejQuery selector for the carousel that is being served by the pager. Defaults to the closestulin their mutual parent element.
Events
The Carousel publishes beforemove and aftermove events. Both events have the following data attached:
currentPosThe current viewIndex at the time of the event.currentItemThe activated slide’s DOM node.currentCarouselThe parent DOM of the carousel.
The carousel also listens to events:
moveToMove the Carousel to a specific slide. Useindexas variable in your event trigger.stopIf the Carousel has the autorun option enabled, this will stop de autorun.prevGo to the previous slide.nextGo to the next slide.pauseIf autorun has been set this will pause the slideshow.resumeIf the Carousel was paused, this will resume it.
Trigger an event like this:
// with paramaters:
$('<carousel_selector>').trigger(
{
type: 'moveTo',
index: 3
});
// without paramaters:
$('<carousel_selector>').trigger('stop');
Public functions
If you don’t want to use event based communication you can also use the public functions exposed by the Carousel.Base class.
moveToMove the Carousel to a specific slide. Useindexas variable in your event trigger.prevGo to the previous slide.nextGo to the next slide.pauseIf autorun has been set this will pause the slideshow.resumeIf the Carousel was paused, this will resume it.stopIf the Carousel has the autorun option enabled, this will stop de autorun.currentPosReturns teh current viewIndex.totalSlidesReturns the number of slides.currentItemThe activated slide’s DOM node.getItemByIndexReturns a slide by viewIndexdestroyRemoves all bindings and stops the Carousel if autorun was set.
Dependencies
This Carousel makes use of jQuery and Morpheus.
Easing
The Carousel uses easing-js to to do it’s fancy easings. Check out which ones you can use on their documentation page.