new Carousel(el [, settings], cb)
- A component for cycling through images, text and other elements, like a carousel.
- Nested carousels are not supported, and generally not compliant with accessibility standards.
- Each carousel should have a unique
id
attribute. - Every initialized carousel gets
MLCarousel
added to the element. For an example see example below. - When setting
arrowKeys: true
, please note you need to focus on the carousel or an element within the carousel for the arrow keys to work correctly. js-carousel-initialized
class name gets added to the carousel element.- You can initialize carousels via
data-carousel
or JavaScript. - Carousel should be formatted as an unordered list
<ul>
and each<li>
should have a class name ofcarousel-slide
, i.e.<li class="carousel-slide"></li>
- See it in action + some notes!
Parameters:
Name | Type | Argument | Description | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
el |
HTMLElement | The carousel element. |
|||||||||||||||||||||||||||||||||||||||||
settings |
Object |
<optional> |
Configuration settings. Properties
|
||||||||||||||||||||||||||||||||||||||||
cb |
function | Callback function after slide has animated. Properties
|
Examples
<div class="carousel" id="customCarousel" data-carousel> <ul> <li class="carousel-slide"><span>0</span><img alt="" src="images/carousel-imgs/giraffe.jpg" /></li> <li class="carousel-slide"><span>1</span><img alt="" src="images/carousel-imgs/eagle.jpg" /></li> <li class="carousel-slide"><span>2</span><img alt="" src="images/carousel-imgs/elephant.jpg" /></li> <li class="carousel-slide"><span>3</span><img alt="" src="images/carousel-imgs/lion.jpg" /></li> </ul> </div>
<div class="carousel animals js-carousel-initialized" data-carousel="autoplay:true:dots:true:arrowKeys:true:autoplay:true:infinite:true:current:1" tabindex="0"> <div class="carousel-viewer" style="overflow: hidden;"> <ul style="left: -620px;"> <li class="carousel-slide"><span>3</span><img alt="" src="images/carousel-imgs/lion.jpg" /></li> <li class="carousel-slide"><span>0</span><img alt="" src="images/carousel-imgs/giraffe.jpg" /></li> <li class="carousel-slide"><span>1</span><img alt="" src="images/carousel-imgs/eagle.jpg" /></li> <li class="carousel-slide"><span>2</span><img alt="" src="images/carousel-imgs/elephant.jpg" /></li> <li class="carousel-slide"><span>3</span><img alt="" src="images/carousel-imgs/lion.jpg" /></li> <li class="carousel-slide"><span>0</span><img alt="" src="images/carousel-imgs/giraffe.jpg" /></li> </ul> </div> <a href="#" class="carousel-nav prev"><i>←</i> <span>Previous</span></a> <a href="#" class="carousel-nav next"><span>Next</span> <i>→</i></a> <div class="carousel-dots"> <ul> <li class=""><a href="#" rel="0">•</a></li> <li class="active"><a href="#" rel="1">•</a></li> <li class=""><a href="#" rel="2">•</a></li> <li class=""><a href="#" rel="3">•</a></li> </ul> </div> </div>
var carousel = new ML.Carousel(ML.El.$('initCarousel'), { dots: true, arrowKeys: true, infinite: true }, function(index, el) { // Slide complete function. // index = current slide index. el = carousel element. console.log(index, el); });
<div class="carousel" id="customCarousel" data-carousel> ... </div> <button id="carouselNext">Next button</button> <script> var carousel = ML.El.$('customCarousel').MLCarousel; carousel.complete(function(index, el) { console.log(index, el); }); ML.El.evt(ML.El.$('carouselNext'), 'click', function(e) { carousel.next(); }); </script>
Methods
-
autoplay(start)
-
Toggle autoplay.
Parameters:
Name Type Description start
Boolean Starts or stops autoplay.
-
destroy()
-
Destroys instance of the carousel.
-
goTo(index)
-
Goes to a specific slide.
Parameters:
Name Type Description index
Number The slide index.
-
init()
-
Initializes the carousel.
-
next()
-
Show the next slide.
-
prev()
-
Show the previous slide.