Skip to main content
100%

Gist 83eee1cf3c019a0fa43d

✓ Published0🌍 Public
Aabernier
Last edited Jun 27, 2017
Created on Jul 17, 2014

This example demonstrates a minimal carousel component built with Backbone.View, jQuery, and Lodash. The view manages a list of items, tracking the active and previous indexes while allowing navigation via prev/next buttons or direct method calls like `go(2)` and `next(3)`. It applies CSS classes such as `active`, `previous`, `forward`, and `backward` to reflect state changes, and triggers a `carouselchange` event whenever the current item changes. The code supports optional looping, configurable step sizes, and explicit selection of item and navigation elements via the `el`, `items`, `prev`, and `next` options.

AI-generated description
<ul class="mycarousel">
  <li>A</li>
  <li class="active">B</li>
  <li>C</li>
</ul>
<nav>
  <a rel="prev" href="javascript:void 0;">Prev</a>
  <a rel="next" href="javascript:void 0;">Next</a>
</nav>

<script>
var mycarousel  = new CarouselView({
  el: '.mycarousel',
  items: 'li',           // optional, default: '>*'
  prev: 'nav a[rel="prev"]', // optional
  next: 'nav a[rel="next"]', // optional
  noloop: true, // optionnal
});

mycarousel.go(2); // go to the 3rd item
mycarousel.prev(); // by default 1-by-1, ie equivalent to: `carousel.prev(1)`
mycarousel.next(3); // or here, 3 next ahead

mycarousel.$prev.click(mycarousel.prev);
mycarousel.$next.click(mycarousel.next);

// mycarousel.currentIndex -- current index
// mycarousel.previousIndex -- current index
// mycarousel.$nth(1) -- 2nd carousel element
// mycarousel.on('carouselchange', function () {alert('carousel has changed!');})
// mycarousel.$current -- current active item (NB: shortcut for `carousel.$nth(carousel.currentIndex)`)
// mycarousel.$previous -- previous active item (NB: shortcut for `carousel.$nth(carousel.previousIndex)`)

// mycarousel.$prev -- previous "arrow" element
// mycarousel.$next -- next "arrow" element
</script>
ISC Licensed

Similar vizzes