z-carousel
is available on npm, published as @benavern/z-carousel
.
The component is available here on npm, you can install it via this command:
npm i @benavern/z-carousel
Then you can use it in your scripts like so in your js/ts module files:
import '@benavern/z-carousel';
<script type="module" src="https://unpkg.com/@benavern/z-carousel"></script>
For example you can use it to display a slider of images
<z-carousel>
<img src="https://picsum.photos/960/540?random=1">
<img src="https://picsum.photos/960/540?random=2">
</z-carousel>
Any HTML will work
<z-carousel>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Inventore vel recusandae quae accusamus, dolore facilis maxime laboriosam quod provident magni in voluptates unde ex. Eveniet ex a expedita numquam soluta?
</p>
<p>
Earum voluptas eius modi consequatur debitis omnis excepturi enim dolorum aut eos. Illum consequatur, commodi, temporibus doloribus unde laboriosam quam adipisci cum, obcaecati incidunt animi dolore quo accusantium possimus voluptatem?
</p>
</z-carousel>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Inventore vel recusandae quae accusamus, dolore facilis maxime laboriosam quod provident magni in voluptates unde ex. Eveniet ex a expedita numquam soluta?
Earum voluptas eius modi consequatur debitis omnis excepturi enim dolorum aut eos. Illum consequatur, commodi, temporibus doloribus unde laboriosam quam adipisci cum, obcaecati incidunt animi dolore quo accusantium possimus voluptatem?
Here is the raw component, you can add anything inside. Each root element will be considered as a slide unless you use one of the defined slots.
To interact with the carousel on (non-touch) desktop, give focus to the component (tab through the document) and start using keyboard arrows.
You can also scroll horizontaly with your mouse if it can do it.
On mobile, you can swipe with your finger from page to page.
You can use the drag
attribute to enable swiping on non-touch devices.
This should not do anything on touch devices as it already is touch friendly
You can add navigation buttons. When a navigation is possible, it can be clicked, otherwize, the button will be disabled.
By default, the navigation buttons are provided with html arrow entities ‹
(‹) and ›
(›).
It is possible to customize the content of the navigation buttons by using the
slot="nav-prev"
and slot="nav-next"
slots.
It is possible to cycle threw the carousel with the infinit
attribute.
It is possible initialize the carousel on a given page with the current-page
attribute
You can decide to display multiple slides per page.
You can add a fraction counter for pagination, this will show the current page number over the total pages number
You can add a little bit of space betwin the slides by using the gap
property.
You can show dots for every page, each dot has the page number as text in it.
The current one is not clickable and the others will slide the carousel to the corresponding page when clicked.
You can disable all interaction on the element with the disabled
attribute.
You can still use the component methods programaticaly.
A change
event fires when the component is about to change page.
This event is a CustomEvent, you have acces to current
and next
page numbers in the event detail object and it is cancelable.
Here is an example of what you could achieve with this component and a little bit of css styling: See CSS