1.0.0 • Published 1 year ago

slideswap v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

slideswap

npm version license mit

Minimal and performant fade slideshow written in pure JavaScript that leans as much as possible on CSS animations. Responsiveness is native, no resize listener. Uses your elements, doesn't detach them or clone them. Provided as ESM and IIFE.

Features

  • No dependencies
  • Small footprint
  • CSS animations, transition duration is set in CSS
  • ESM and IIFE
  • Adaptive height (also animated)
  • Responsive (no resize listener)
  • Uses your elements, doesn't detach them or clone them
  • Supports touch events (swipe) and mouse events (swipe simulation)
  • Supports infinite loop
  • Custom next and prev buttons
  • No CLS (Cumulative Layout Shift) if you set initial classes and start from the first slide

Demo

https://stamat.github.io/slideswap/

Installation

NPM

npm install slideswap

Yarn

yarn add slideswap

Include the module in your project

import Slideswap from 'slideswap'; // If you have node_modules resolution, if not than add the path to the module

Include the SCSS file in your project or copy the styles from it, it's a very small CSS inside

@import 'slideswap'; // If you have node_modules resolution, if not than add the path to the SCSS file

CDN

<script src="https://unpkg.com/slideswap/dist/slideswap.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/slideswap/dist/slideswap.min.css">

Usage

Basic usage

<div id="slider-controls-1" class="slideswap-controls">
  <button class="js-slideswap-prev">Prev</button>
  <button class="js-slideswap-next">Next</button>
</div>
<div id="slider-1" class="slideswap-slides">
  <div class="slideswap-slide">Slide 1</div>
  <div class="slideswap-slide">Slide 2</div>
  <div class="slideswap-slide">Slide 3</div>
</div>

<script type="text/javascript">
  const slider = new Slideswap('#slider-1', {
    next: '#slider-controls-1 .js-slideswap-next',
    prev: '#slider-controls-1 .js-slideswap-prev',
    infinite: true
  });

  slider.element.addEventListener('slideswap:beforechange', (e) => {
    console.log(e.detail);
  });

  document.addEventListener('slideswap:beforechange', (e) => {
    console.log(e.detail);
  });

  slider.element.addEventListener('slideswap:change', (e) => {
    console.log(e.detail);
  });

  document.addEventListener('slideswap:change', (e) => {
    console.log(e.detail);
  });

  // slider.next(); - to go to the next slide
  // slider.previous(); - to go to the previous slide
  // slider.setCurrentSlide(index); - to go to a specific slide
  // slider.getCurrentSlide(); - to get the current slide index
  // slider.getNextIndex(); - to get the next slide index
  // slider.getPreviousIndex(); - to get the previous slide index
  // slider.getCurrentIndex(); - to get the current slide index
  // slider.getSlidesCount(); - to get the total number of slides
  // slider.destroy(); - to destroy the instance
  // slider.init(element, options); - to reinit the instance (if you have destroyed it)
</script>

The constructor accepts two arguments, the first one is the element or selector for the element and the second one is the options object.

new Slideswap(element, options);

Options

OptionTypeDefaultDescription
infiniteBooleanfalseWhether or not the slideshow should loop infinitely
activeClassStringslideswap-current-slideThe class to apply to the current slide
slideSelectorString:scope > *The selector for the slides
startNumber0The index of the slide to start on
adaptiveHeightBooleantrueWhether or not the slideshow should adapt to the height of the current slide
nextHTMLElement or StringnullThe element to use as the next button or selector for the next button
prevHTMLElement or StringnullThe element to use as the previous button or selector for the previous button
imageSelectorString:scope > imgThe selector for the images in the slides
swipeBooleantrueWhether or not the slideshow should be swipeable
swipeClassStringslideswap-has-swipeThe class to apply to the slideshow when it is swipeable
swipeActiveClassStringslideswap-swipe-activeThe class to apply to the slideshow when it is being swiped
swipeThresholdNumber100The minimum distance the swipe must travel to trigger a slide change
swipeTimeThresholdNumber1000The maximum amount of time the swipe can take to trigger a slide change in milliseconds. 0 disables this.

Events

Events are triggered for each instance element and for the document. The event detail contains the object the target slide index and the previous slide index.

EventDescription
slideswap:initFires after the instance is initialized.
slideswap:beforechangeFires before the slide changes. The event detail contains the current slide index and the next slide index.
slideswap:changeFires after the slide changes. The event detail contains the current slide index and the previous slide index.
slideswap:destroyFires after the instance is destroyed.

Methods

MethodDescription
next()Goes to the next slide.
prev()Goes to the previous slide.
setCurrentSlide(index)Goes to a specific slide.
getCurrentSlide()Returns the current slide index.
getNextIndex()Returns the next slide index.
getPreviousIndex()Returns the previous slide index.
getCurrentIndex()Returns the current slide index.
getSlidesCount()Returns the total number of slides.
add(slide, index)Adds a slide. Slide parameter is an HTMLElement and index is insert before index and is optional
remove(index)Removes a slide. If index is not provided or invalid it will remove the last slide
destroy()Destroys the instance.
init(element, options)Reinitializes the instance.

Public properties

PropertyDescription
elementThe instance element.
optionsThe instance options.
slidesThe instance slides.
currentIndexThe current slide index.

TODO

  • Add autoplay
  • Add bullet navigation
  • Add keyboard navigation
  • Fix animation on current slide remove
  • Wheel event support?