Carousel β
Provides a scroll carousel that uses native CSS scroll-snap
property.
Usage β
@import "winduum/src/components/carousel/index.css" layer(components);
<div class="c-carousel flex gap-4 items-center w-full">
<button class="ui-btn circle muted" data-action="click->c-carousel#prev" aria-label="Prev">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M10.5 19.5 3 12m0 0 7.5-7.5M3 12h18" />
</svg>
</button>
<div class="c-carousel-content gap-2 w-full" data-c-carousel-target="content">
<div class="ui-image w-full rounded-md aspect-square">
<img src="https://via.placeholder.com/512" alt="" loading="lazy">
</div>
<div class="ui-image w-full rounded-md aspect-square">
<img src="https://via.placeholder.com/512" alt="" loading="lazy">
</div>
</div>
<button class="ui-btn circle muted" data-action="click->c-carousel#next" aria-label="Next">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3" />
</svg>
</button>
</div>
<script type="module">
import { scrollPrev, scrollNext, observeCarousel } from "winduum/src/components/carousel";
const contentElement = document.querySelector('[data-c-carousel-target="content"]')
const prevElement = document.querySelector('[data-action="click->c-carousel#prev"]')
const nextElement = document.querySelector('[data-action="click->c-carousel#next"]')
observeCarousel(contentElement)
prevElement.addEventListener('click', () => scrollPrev(contentElement))
nextElement.addEventListener('click', () => scrollNext(contentElement))
</script>
Variants β
Installation β
Follow instructions for individual framework usage below
Examples β
Basic β
<div class="c-carousel flex gap-4 items-center w-full">
<button class="ui-btn circle muted" data-action="click->c-carousel#prev" aria-label="Prev">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M10.5 19.5 3 12m0 0 7.5-7.5M3 12h18" />
</svg>
</button>
<div class="c-carousel-content gap-2 w-full" data-c-carousel-target="content">
<div class="ui-image w-full rounded-md aspect-square">
<img src="https://via.placeholder.com/512" alt="" loading="lazy">
</div>
<div class="ui-image w-full rounded-md aspect-square">
<img src="https://via.placeholder.com/512" alt="" loading="lazy">
</div>
</div>
<button class="ui-btn circle muted" data-action="click->c-carousel#next" aria-label="Next">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3" />
</svg>
</button>
</div>
<script type="module">
import { scrollPrev, scrollNext, observeCarousel } from "winduum/src/components/carousel";
const contentElement = document.querySelector('[data-c-carousel-target="content"]')
const prevElement = document.querySelector('[data-action="click->c-carousel#prev"]')
const nextElement = document.querySelector('[data-action="click->c-carousel#next"]')
observeCarousel(contentElement)
prevElement.addEventListener('click', () => scrollPrev(contentElement))
nextElement.addEventListener('click', () => scrollNext(contentElement))
</script>
Full β
<div class="c-carousel flex-col gap-4">
<div class="flex justify-center">
<div data-c-carousel-target="counterMin"></div>/
<div data-c-carousel-target="counterMax"></div>
</div>
<div class="flex gap-4 items-center">
<button class="ui-btn circle muted" data-action="click->c-carousel#prev" aria-label="Prev">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M10.5 19.5 3 12m0 0 7.5-7.5M3 12h18" />
</svg>
</button>
<div class="c-carousel-content gap-4 w-full" data-c-carousel-target="content">
<div class="ui-image rounded-md">
<img src="https://via.placeholder.com/160x240" alt="" loading="lazy" width="160" height="240">
</div>
<div class="ui-image rounded-md">
<img src="https://via.placeholder.com/160x240" alt="" loading="lazy" width="160" height="240">
</div>
<div class="ui-image rounded-md">
<img src="https://via.placeholder.com/160x240" alt="" loading="lazy" width="160" height="240">
</div>
<div class="ui-image rounded-md">
<img src="https://via.placeholder.com/160x240" alt="" loading="lazy" width="160" height="240">
</div>
<div class="ui-image rounded-md">
<img src="https://via.placeholder.com/160x240" alt="" loading="lazy" width="160" height="240">
</div>
<div class="ui-image rounded-md">
<img src="https://via.placeholder.com/160x240" alt="" loading="lazy" width="160" height="240">
</div>
</div>
<button class="ui-btn circle muted" data-action="click->c-carousel#next" aria-label="Next">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3" />
</svg>
</button>
</div>
<div class="justify-center flex gap-1.5 h-4 py-4" data-c-carousel-target="pagination"></div>
<progress class="ui-progress sm" value="50" max="100" data-c-carousel-target="progress"></progress>
</div>
<script type="module">
import { scrollPrev, scrollNext, observeCarousel, dragCarousel, paginationCarousel, scrollCarousel } from "winduum/src/components/carousel";
const contentElement = document.querySelector('[data-c-carousel-target="content"]')
const paginationElement = document.querySelector('[data-c-carousel-target="pagination"]')
const progressElement = document.querySelector('[data-c-carousel-target="progress"]')
const counterMinElement = document.querySelector('[data-c-carousel-target="counterMin"]')
const counterMaxElement = document.querySelector('[data-c-carousel-target="counterMax"]')
const prevElement = document.querySelector('[data-action="click->c-carousel#prev"]')
const nextElement = document.querySelector('[data-action="click->c-carousel#next"]')
const pagination = {
element: paginationElement,
activeClass: 'bg-error',
itemContent: '<div class="dot"></div>'
}
const scrollOptions = {
counterMinElement,
counterMaxElement,
progressElement,
pagination
}
dragCarousel(contentElement)
observeCarousel(contentElement)
paginationCarousel(contentElement, pagination)
scrollCarousel(contentElement, scrollOptions)
contentElement.addEventListener('scroll', () => scrollCarousel(contentElement, scrollOptions), { passive: true })
prevElement.addEventListener('click', () => scrollPrev(contentElement))
nextElement.addEventListener('click', () => scrollNext(contentElement))
</script>
Javascript API β
scrollTo
β
- Type:
(element: HTMLElement | Element, index: number) => void
- Kind:
sync
Scroll to a snap item by its index.
scrollNext
β
- Type:
(element: HTMLElement | Element) => void
- Kind:
sync
Scroll to a next snap item.
scrollPrev
β
- Type:
(element: HTMLElement | Element) => void
- Kind:
sync
Scroll to a previous snap item.
getItemCount
β
- Type:
(element: HTMLElement | Element, scrollWidth: number, mathFloor: boolean) => number
- Kind:
sync
Get number of possible scrolls inside the carousel.
observeCarousel
β
- Type:
(element: HTMLElement | Element, options?: ObserveCarouselOptions) => void
- Kind:
sync
Adds an observer for the carousel. Adds properties _observer
and _activeIndex
to the DOM of the carousel element
.
ObserveCarouselOptions β
visibleClass β
- Type:
string
- Default:
visible
A class that is added to the carousel items once they are visible.
observerOptions β
- Type:
IntersectionObserverInit
- Default:
{ threshold: 0.5 }
Additional options confugration for the observer.
dragCarousel
β
- Type:
(element: HTMLElement | Element, options?: DragCarouselOptions) => void
- Kind:
sync
Adds a mouse dragging capability to the carousel.
DragCarouselOptions β
activeClass β
- Type:
string
- Default:
grabbing
paginationCarousel
β
- Type:
(element: HTMLElement | Element, options?: PaginationCarouselOptions) => void
- Kind:
sync
Inserts pagination indicators for the carousel to the desired element.
PaginationCarouselOptions β
element β
- Type:
HTMLElement | Element
- Default:
undefined
itemContent β
- Type:
string
- Default:
<div aria-hidden="true"></div>
activeClass β
- Type:
string
- Default:
active
autoplayCarousel
β
- Type:
(element: HTMLElement | Element, options?: AutoplayCarouselOptions) => void
- Kind:
sync
Adds an autoplay for the carousel.
AutoplayCarouselOptions β
delay β
- Type:
number
- Default:
4000
Delay in ms.
pauseElements β
- Type:
HTMLElement[] | Element[]
- Default:
[]
Which elements should pause the autoplay upon hover.
scrollCarousel
β
- Type:
(element: HTMLElement | Element, options?: ScrollCarouselOptions) => void
- Kind:
sync
A helper function that updates various carousel states upon scroll.
ScrollCarouselOptions β
observe β
- Type:
ObserveCarouselOptions
- Default:
undefined
pagination β
- Type:
PaginationCarouselOptions
- Default:
{ activeClass: 'active' }
progressElement β
- Type:
HTMLProgressElement | Element
- Default:
undefined
counterMinElement β
- Type:
HTMLElement | Element
- Default:
undefined
counterMaxElement β
- Type:
HTMLElement | Element
- Default:
undefined