1.11.2 • Published 4 months ago

snap-carousel.js v1.11.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Snap Carousel

npm version gzip size License: MIT

A modern, lightweight (5.4kB gzip) JavaScript carousel component that leverages the power of CSS scroll-snap and custom element shadow DOM. Perfect for creating responsive, performant, and customizable image galleries, product carousels, and content sliders.

View Demo | Documentation | Installation | Usage

Table of Contents

Features

  • ✨ Lightweight and performant - only 5.4kB gzipped
  • 🎯 Previous/Next navigation buttons
  • 📍 Pagination indicators
  • 🔢 Pager display (e.g., "1 of 6")
  • 🔄 loop (CSS-based, no element cloning)
  • ⏯️ Autoplay with hover pause
  • 📱 Fully responsive configuration
  • 🎨 Customizable through shadow DOM
  • 🚀 Zero dependencies

Installation

npm install snap-carousel.js
# or
yarn add snap-carousel.js

Documentation

Basic Usage

<snap-carousel>
  <div slot="scroller">
    <div>Slide 1</div>
    <div>Slide 2</div>
    <div>Slide 3</div>
  </div>
</snap-carousel>

<script>
  import 'snap-carousel.js';
</script>

ES Modules Usage

// Import everything
import { SnapCarousel } from 'snap-carousel.js';

// Register the web component
customElements.define('snap-carousel', SnapCarousel);

// Or import specific features
import { BaseCarousel, createCarousel } from 'snap-carousel.js/base-carousel';
import { NavFeature } from 'snap-carousel.js/features/nav';
import { PagerFeature } from 'snap-carousel.js/features/pager';

// Create custom carousel with only navigation and pager
const CustomCarousel = createCarousel(NavFeature, PagerFeature);
customElements.define('custom-carousel', CustomCarousel);

Configuration Options

OptionTypeDefaultDescription
displayednumber1Number of items visible in the viewport at once
perPagenumber1Number of items to scroll per navigation action
gapstring/number0Space between carousel items (CSS units, e.g., "1rem", "16px")
paddingstring/number0Padding around the carousel viewport (CSS units)
controlsbooleanfalseShow previous/next navigation buttons
navbooleanfalseShow navigation dots for direct slide access
pagerbooleanfalseShow current/total slides counter
loopbooleanfalseEnable infinite looping of carousel items
autoplaynumber0Autoplay interval in milliseconds (0 to disable)
usePausebooleantruePause autoplay on hover
behaviorstring"smooth"Scroll behavior ("smooth" or "auto")
stopbooleanfalseForce stopping at each step (scroll-snap-stop: always)
verticalbooleanfalseEnable vertical scrolling mode
responsivearray[]Breakpoint-specific settings
syncstringnullSelector for other carousels to sync with

Example responsive configuration:

<snap-carousel
  displayed="1"
  responsive='[{
    "breakpoint": 768,
    "settings": {
      "displayed": 2,
      "perPage": 2
    }
  }, {
    "breakpoint": 1024,
    "settings": {
      "displayed": 3,
      "perPage": 3
    }
  }]'>
  <div slot="scroller">
    <!-- carousel items -->
  </div>
</snap-carousel>

See the Demo page for more examples and configuration options.

Examples

<!-- Basic carousel with navigation -->
<snap-carousel controls nav>
  <div slot="scroller">
    <img src="slide1.jpg" alt="Slide 1">
    <img src="slide2.jpg" alt="Slide 2">
    <img src="slide3.jpg" alt="Slide 3">
  </div>
</snap-carousel>

<!-- Autoplay carousel with pagination -->
<snap-carousel autoplay="5000" nav pager>
  <div slot="scroller">
    <div>Content 1</div>
    <div>Content 2</div>
    <div>Content 3</div>
  </div>
</snap-carousel>

Browser Support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Edge (latest)

Requires browsers with support for:

  • Custom Elements v1
  • Shadow DOM v1
  • CSS Scroll Snap

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.