0.0.1 • Published 3 years ago

@signalchain/looped v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Maintenance Status NPM JavaScript Style Guide

"Looped" Carousel

The "bring your own styles", responsive/dynamic carousel for JavaScript. See examples in the github.

Exposes handy methods like .next() and .prev() on elements that can be easily wired to event handlers in the onInit method.

An easy to configure object syntax allows you to override defaults setting with one line of code. (See defaults in the table below or in the examples.)

looped demo

Installation

npm install looped

or

yarn add looped

<div class="carousel">
  <div>slide 1</div>
  <div>slide 2</div>
  <div>slide 3</div>
  <div>slide 4</div>
</div>
import Carousel from 'looped';

// Will initialize with default settings
new Carousel({
  selector: '.carousel'
});

Default settings

new Carousel({
  selector: '.carousel',
  transitionDuration: 450,
  easing: 'ease-out',
  perPage: 1,
  gap: 0,
  startIndex: 0,
  draggable: true,
  multipleDrag: true,
  threshold: 20,
  loop: true,
  animate: true,
  intervalDuration: 4250,
  rtl: false,
  onInit: () => {},
  onChange: () => {},
  destroy: () => {},
});

Responsive slide count

Pass an object to perPage and prefix window width with w. Since the default is 1, the properties are applied from narrowest window width to widest window width.

const selector = '.carousel';
const carousel = document.querySelector(selector);
const config = {
  ...
  perPage: {
    // Falls back to the default of 1 below 750px
    w750: 3,
    w1200: 5,
    w1400: 6,
  },
  ...
}

const initCarousel = carousel &&
  new Carousel({
    selector,
    ...config,
  })

// Grab your buttons from the DOM
const prev = document.querySelector('.prev')
const next = document.querySelector('.next')

prev.addEventListener('click', () => {
  initCarousel.prev()
})

next.addEventListener('click', () => {
  initCarousel.next()
})

initCarousel()

Available properties

PropData TypeDefaultRequiredDescription
selectorStringN/AtrueEnable selector
transitionDurationNumber450falseThe time (in ms) between when the slide chip starts it's transition, to when it finishes moving.
easinStringease-outfalseThe "ease" value on the transition property.
perPageNumber or Object1falseThe number of slides to be shown per page. See example for responsive carousel.
gapNumber0falseThe space between slides.
startIndexNumber0falseThe index of the starting index
draggableBooleantruefalseUse dragging and touch swiping
multipleDragBooleantruefalseAllow dragging to move multiple slides.
thresholdNumber20falseHow far you have to swipe/drag (in px) to go to the next slide.
loopBooleantruefalseEnable slides to loop.
animateBooleantruefalseAuto-play slides on load.
intervalDurationnumber4250falseThe amount of time between next slide call.
rtlBooleanfalsefalseEnables layout for languages written from right to left.
onInitFunctionfalseRuns immediately after initialization.
onChangeFunctionfalseRuns after slide change.
destroyFunctionfalseRuns when carousel is unmounted.

Common Gotchas

Gap throws off my image spacing. What gives?

Solution: Add this to your CSS

html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
}

How to contribute

If you have unanswered questions, would like help with enhancing or debugging the plugin, reach out @ jameygleason.com.

Please see CONTRIBUTING for details.

Credits

This carousel would not be possible without the fine work done on Siema.

NPM

https://www.npmjs.com/package/looped

License

The MIT License (MIT). Please see License File for more information.