2022-08-06 08:45:45 +00:00
# Carousel
[component-header:sl-carousel]
2023-02-23 16:50:18 -05:00
```html preview
<sl-carousel pagination navigation mouse-dragging loop>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</sl-carousel-item>
</sl-carousel>
```
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const App = () => (
<>
<SlCarousel pagination mouse-dragging>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
</>
);
```
## Examples
### Pagination
Use the `pagination` attribute to show the total number of slides and the current slide as a set of interactive dots.
2022-08-06 08:45:45 +00:00
```html preview
2023-02-23 16:50:18 -05:00
<sl-carousel pagination>
2022-08-06 08:45:45 +00:00
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
</sl-carousel>
2023-02-23 16:50:18 -05:00
```
2022-08-06 08:45:45 +00:00
2023-02-24 15:10:54 -05:00
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const App = () => (
<SlCarousel pagination>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
);
```
2023-02-23 16:50:18 -05:00
### Navigation
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
Use the `navigation` attribute to show previous and next buttons.
```html preview
<sl-carousel navigation>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</sl-carousel-item>
</sl-carousel>
2022-08-06 08:45:45 +00:00
```
2023-02-24 15:10:54 -05:00
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const App = () => (
<SlCarousel navigation>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
);
```
2023-02-23 16:50:18 -05:00
### Looping
2023-01-08 13:36:24 +01:00
2023-02-23 16:50:18 -05:00
By default, the carousel will not advanced beyond the first and last slides. You can change this behavior and force the carousel to "wrap" with the `loop` attribute.
2023-01-08 13:36:24 +01:00
2023-02-23 16:50:18 -05:00
```html preview
<sl-carousel loop navigation pagination>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</sl-carousel-item>
</sl-carousel>
```
2023-01-08 13:36:24 +01:00
2023-02-24 15:10:54 -05:00
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const App = () => (
<SlCarousel loop navigation pagination>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
);
```
2023-02-23 16:50:18 -05:00
### Autoplay
The carousel will automatically advance when the `autoplay` attribute is used. To change how long a slide is shown before advancing, set `autoplay-interval` to the desired number of milliseconds. For best results, use the `loop` attribute when autoplay is enabled. Note that autoplay will pause while the user interacts with the carousel.
```html preview
<sl-carousel autoplay loop pagination>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</sl-carousel-item>
</sl-carousel>
2023-01-08 13:36:24 +01:00
```
2023-02-24 15:10:54 -05:00
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const App = () => (
<SlCarousel autoplay loop pagination>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
);
```
2023-02-23 16:50:18 -05:00
### Mouse Dragging
The carousel uses [scroll snap ](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Scroll_Snap ) to position slides at various snap positions. This allows users to scroll through the slides very naturally, especially on touch devices. Unfortunately, desktop users won't be able to click and drag with a mouse, which can feel unnatural. Adding the `mouse-dragging` attribute can help with this.
This example is best demonstrated using a mouse. Try clicking and dragging the slide to move it. Then toggle the switch and try again.
```html preview
<div class="mouse-dragging">
<sl-carousel pagination>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 16:50:18 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</sl-carousel-item>
</sl-carousel>
<sl-divider></sl-divider>
<sl-switch>Enable mouse dragging</sl-switch>
</div>
2023-01-08 13:36:24 +01:00
<script>
2023-02-23 16:50:18 -05:00
const container = document.querySelector('.mouse-dragging');
const carousel = container.querySelector('sl-carousel');
const toggle = container.querySelector('sl-switch');
2023-01-08 13:36:24 +01:00
2023-02-23 16:50:18 -05:00
toggle.addEventListener('sl-change', () => {
carousel.toggleAttribute('mouse-dragging', toggle.checked);
});
</script>
```
2022-08-06 08:45:45 +00:00
2023-02-24 15:10:54 -05:00
```jsx react
import { useState } from 'react';
import { SlCarousel, SlCarouselItem, SlDivider, SlSwitch } from '@shoelace -style/shoelace/dist/react';
const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
return (
<>
<SlCarousel navigation mouseDragging={isEnabled}>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
<SlDivider></SlDivider>
<SlSwitch checked={isEnabled} onSlInput={() => setIsEnabled(!isEnabled)}>
Enable mouse dragging
</SlSwitch>
</>
);
};
```
2023-02-23 16:50:18 -05:00
### Multiple Slides Per View
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
The `slides-per-view` attribute makes it possible to display multiple slides at a time. You can also use the `slides-per-move` attribute to advance more than once slide at a time, if desired.
2022-08-06 08:45:45 +00:00
```html preview
2023-02-24 15:10:54 -05:00
<sl-carousel navigation pagination slides-per-page="2" slides-per-move="2">
2023-02-23 16:50:18 -05:00
<sl-carousel-item style="background: var(--sl-color-red-200);">Slide 1</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-orange-200);">Slide 2</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-yellow-200);">Slide 3</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-green-200);">Slide 4</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-blue-200);">Slide 5</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-violet-200);">Slide 6</sl-carousel-item>
2022-08-06 08:45:45 +00:00
</sl-carousel>
```
2023-01-08 13:36:24 +01:00
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const App = () => (
2023-02-24 15:10:54 -05:00
<SlCarousel navigation pagination slidesPerPage={2} slidesPerMove={2}>
<SlCarouselItem style={{ background: 'var(--sl-color-red-200)' }}>Slide 1</SlCarouselItem>
<SlCarouselItem style={{ background: 'var(--sl-color-orange-200)' }}>Slide 2</SlCarouselItem>
<SlCarouselItem style={{ background: 'var(--sl-color-yellow-200)' }}>Slide 3</SlCarouselItem>
<SlCarouselItem style={{ background: 'var(--sl-color-green-200)' }}>Slide 4</SlCarouselItem>
<SlCarouselItem style={{ background: 'var(--sl-color-blue-200)' }}>Slide 5</SlCarouselItem>
<SlCarouselItem style={{ background: 'var(--sl-color-violet-200)' }}>Slide 6</SlCarouselItem>
2023-01-08 13:36:24 +01:00
</SlCarousel>
);
```
2023-02-23 16:50:18 -05:00
### Adding and Removing Slides
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
The content of the carousel can be changed by appending or removing carousel items. The carousel will update itself automatically.
2022-08-06 08:45:45 +00:00
```html preview
<sl-carousel class="dynamic-carousel" pagination navigation>
2023-02-23 16:50:18 -05:00
<sl-carousel-item style="background: var(--sl-color-red-200)">Slide 1</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-orange-200)">Slide 2</sl-carousel-item>
<sl-carousel-item style="background: var(--sl-color-yellow-200)">Slide 3</sl-carousel-item>
2022-08-06 08:45:45 +00:00
</sl-carousel>
2023-02-23 16:50:18 -05:00
2022-08-06 08:45:45 +00:00
<div class="carousel-options">
<sl-button id="dynamic-add">Add slide</sl-button>
<sl-button id="dynamic-remove">Remove slide</sl-button>
</div>
2023-02-23 16:50:18 -05:00
2022-08-06 08:45:45 +00:00
<style>
.dynamic-carousel {
--aspect-ratio: 3 / 2;
}
2023-02-23 16:50:18 -05:00
.dynamic-carousel ~ .carousel-options {
display: flex;
justify-content: center;
gap: var(--sl-spacing-x-small);
margin-top: var(--sl-spacing-large);
}
2022-08-06 08:45:45 +00:00
.dynamic-carousel sl-carousel-item {
flex: 0 0 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: var(--sl-font-size-2x-large);
}
</style>
2023-02-23 16:50:18 -05:00
2022-08-06 08:45:45 +00:00
<script>
(() => {
const dynamicCarousel = document.querySelector('.dynamic-carousel');
const dynamicAdd = document.querySelector('#dynamic -add');
const dynamicRemove = document.querySelector('#dynamic -remove');
2023-02-23 16:50:18 -05:00
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet'];
let colorIndex = 2;
2022-08-06 08:45:45 +00:00
const addSlide = () => {
const slide = document.createElement('sl-carousel-item');
2023-02-23 16:50:18 -05:00
const color = colors[++colorIndex % colors.length];
2022-08-06 08:45:45 +00:00
slide.innerText = `Slide ${dynamicCarousel.children.length + 1}` ;
2023-02-23 16:50:18 -05:00
slide.style.setProperty('background', `var(--sl-color-${color}-200)` );
2022-08-06 08:45:45 +00:00
dynamicCarousel.appendChild(slide);
};
const removeSlide = () => {
const slide = dynamicCarousel.children[dynamicCarousel.children.length - 1];
slide.remove();
2023-02-23 16:50:18 -05:00
colorIndex--;
2022-08-06 08:45:45 +00:00
};
dynamicAdd.addEventListener('click', addSlide);
dynamicRemove.addEventListener('click', removeSlide);
})();
</script>
```
2023-01-08 13:36:24 +01:00
```jsx react
import { useState } from 'react';
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const css = `
.dynamic-carousel {
--aspect-ratio: 3 / 2;
}
2023-02-23 16:50:18 -05:00
.dynamic-carousel ~ .carousel-options {
display: flex;
justify-content: center;
margin-top: var(--sl-spacing-large);
}
2023-01-08 13:36:24 +01:00
.dynamic-carousel sl-carousel-item {
flex: 0 0 100%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: var(--sl-font-size-2x-large);
}
`;
const App = () => {
const [slides, setSlides] = useState(['#204ed8 ', '#be133d ', '#6e28d9 ']);
2023-02-23 16:50:18 -05:00
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet'];
2023-01-08 13:36:24 +01:00
const addSlide = () => {
setSlides([...slides, getRandomColor()]);
};
const removeSlide = () => {
setSlides(slides.slice(0, -1));
};
return (
<>
<SlCarousel className="dynamic-carousel" pagination navigation>
{slides.map((color, i) => (
2023-02-23 16:50:18 -05:00
<SlCarouselItem style={{ background: colors[i % colors.length }}>
Slide {i}
</SlCarouselItem>
2023-01-08 13:36:24 +01:00
))}
</SlCarousel>
2023-02-23 16:50:18 -05:00
2023-01-08 13:36:24 +01:00
<div className="carousel-options">
<SlButton onClick={addSlide}>Add slide</SlButton>
<SlButton onClick={removeSlide}>Remove slide</SlButton>
</div>
2023-02-23 16:50:18 -05:00
2023-01-08 13:36:24 +01:00
<style>{css}</style>
</>
);
};
```
2023-02-23 16:50:18 -05:00
### Vertical Scrolling
2022-08-06 08:45:45 +00:00
Setting the `orientation` attribute to `vertical` , will make the carousel laying out vertically, making it
possible for the user to scroll it up and down. In case of heterogeneous content, for example images of different sizes,
it's important to specify a predefined height to the carousel through CSS.
```html preview
2023-02-23 16:50:18 -05:00
<sl-carousel class="vertical" pagination orientation="vertical">
2022-08-06 08:45:45 +00:00
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
</sl-carousel>
<style>
.vertical {
max-height: 400px;
}
.vertical::part(base) {
grid-template-areas: 'slides slides pagination';
}
.vertical::part(pagination) {
flex-direction: column;
}
.vertical::part(navigation) {
transform: rotate(90deg);
display: flex;
}
</style>
```
2023-01-08 13:36:24 +01:00
```jsx react
import { SlCarousel, SlCarouselItem } from '@shoelace -style/shoelace/dist/react';
const css = `
.vertical {
max-height: 400px;
}
.vertical::part(base) {
grid-template-areas: 'slides slides pagination';
}
.vertical::part(pagination) {
flex-direction: column;
}
.vertical::part(navigation) {
transform: rotate(90deg);
display: flex;
}
`;
const App = () => (
<>
<SlCarousel className="vertical" loop pagination orientation="vertical">
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
</SlCarousel>
<style>{css}</style>
</>
);
```
2023-02-23 16:50:18 -05:00
### Aspect Ratio
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
Use the `--aspect-ratio` custom property to customize the size of the carousel's viewport.
2022-08-06 08:45:45 +00:00
```html preview
2023-02-24 15:10:54 -05:00
<sl-carousel class="aspect-ratio" navigation pagination style="--aspect-ratio: 3/2;">
2022-08-06 08:45:45 +00:00
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
</sl-carousel>
<sl-divider></sl-divider>
2023-02-23 16:50:18 -05:00
<sl-select label="Aspect ratio" name="aspect" value="3/2">
<sl-option value="1/1">1/1</sl-option>
<sl-option value="3/2">3/2</sl-option>
<sl-option value="16/9">16/9</sl-option>
2022-08-06 08:45:45 +00:00
</sl-select>
<script>
(() => {
const carousel = document.querySelector('sl-carousel.aspect-ratio');
2023-02-23 16:50:18 -05:00
const aspectRatio = document.querySelector('sl-select[name="aspect"]');
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
aspectRatio.addEventListener('sl-change', () => {
carousel.style.setProperty('--aspect-ratio', aspectRatio.value);
2022-08-06 08:45:45 +00:00
});
})();
</script>
```
2023-01-08 13:36:24 +01:00
```jsx react
import { useState } from 'react';
2023-02-23 16:50:18 -05:00
import { SlCarousel, SlCarouselItem, SlDivider, SlSelect, SlOption } from '@shoelace -style/shoelace/dist/react';
2023-01-08 13:36:24 +01:00
const App = () => {
const [aspectRatio, setAspectRatio] = useState('3/2');
return (
<>
<SlCarousel className="aspect-ratio" navigation pagination style={{ '--aspect-ratio': aspectRatio }}>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2023-01-08 13:36:24 +01:00
/>
</SlCarouselItem>
</SlCarousel>
<SlDivider />
<SlSelect
label="Aspect ratio"
name="aspect"
value={aspectRatio}
onSlChange={event => setAspectRatio(event.target.value)}
>
2023-02-23 16:50:18 -05:00
<SlOption value="1 / 1">1 / 1</SlOption>
<SlOption value="3 / 2">3 / 2</SlOption>
<SlOption value="16 / 9">16 / 9</SlOption>
2023-01-08 13:36:24 +01:00
</SlSelect>
<style>{css}</style>
</>
);
};
```
2023-02-23 16:50:18 -05:00
### Scroll Hint
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
Use the `--scroll-hint` attribute to add inline padding in horizontal carousels and block padding in vertical carousels. Setting a padding will make the closest slides slightly visible, hinting that there are more items in the carousel.
2022-08-06 08:45:45 +00:00
```html preview
2023-02-23 16:50:18 -05:00
<sl-carousel class="scroll-hint" pagination style="--scroll-hint: 10%;">
2022-08-06 08:45:45 +00:00
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
</sl-carousel>
```
2023-01-08 13:36:24 +01:00
```jsx react
import { useState } from 'react';
import { SlCarousel, SlCarouselItem, SlDivider, SlRange } from '@shoelace -style/shoelace/dist/react';
2023-02-24 15:10:54 -05:00
const App = () => (
<>
<SlCarousel className="scroll-hint" pagination style={{ '--scroll-hint': '10%' }}>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/mountains.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/waterfall.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/sunset.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/field.jpg"
/>
</SlCarouselItem>
<SlCarouselItem>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-24 15:10:54 -05:00
src="/assets/examples/carousel/valley.jpg"
/>
</SlCarouselItem>
</SlCarousel>
</>
);
2023-01-08 13:36:24 +01:00
```
2023-02-23 16:50:18 -05:00
### Gallery Example
2022-08-06 08:45:45 +00:00
2023-02-23 16:50:18 -05:00
The carousel has a robust API that makes it possible to extend and customize. This example syncs the active slide with a set of thumbnails, effectively creating a gallery-style carousel.
2022-08-06 08:45:45 +00:00
```html preview
<sl-carousel class="carousel-thumbnails" navigation loop>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun shines on the mountains and trees (by Adam Kool on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/mountains.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A waterfall in the middle of a forest (by Thomas Kelly on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/waterfall.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="The sun is setting over a lavender field (by Leonard Cotte on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/sunset.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A field of grass with the sun setting in the background (by Sapan Patel on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/field.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
<sl-carousel-item>
<img
2023-02-24 15:56:31 -05:00
alt="A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash)"
2023-02-23 14:39:05 -05:00
src="/assets/examples/carousel/valley.jpg"
2022-08-06 08:45:45 +00:00
/>
</sl-carousel-item>
</sl-carousel>
2023-02-23 16:50:18 -05:00
2022-08-06 08:45:45 +00:00
<div class="thumbnails">
<div class="thumbnails__scroller">
2023-02-24 15:56:31 -05:00
<img alt="Thumbnail by 1" class="thumbnails__image active" src="/assets/examples/carousel/mountains.jpg" />
<img alt="Thumbnail by 2" class="thumbnails__image" src="/assets/examples/carousel/waterfall.jpg" />
<img alt="Thumbnail by 3" class="thumbnails__image" src="/assets/examples/carousel/sunset.jpg" />
<img alt="Thumbnail by 4" class="thumbnails__image" src="/assets/examples/carousel/field.jpg" />
<img alt="Thumbnail by 5" class="thumbnails__image" src="/assets/examples/carousel/valley.jpg" />
2022-08-06 08:45:45 +00:00
</div>
</div>
2023-02-23 16:50:18 -05:00
2022-08-06 08:45:45 +00:00
<style>
.carousel-thumbnails {
--slide-aspect-ratio: 3 / 2;
}
.thumbnails {
display: flex;
justify-content: center;
}
.thumbnails__scroller {
display: flex;
gap: var(--sl-spacing-small);
overflow-x: auto;
scrollbar-width: none;
scroll-behavior: smooth;
scroll-padding: var(--sl-spacing-small);
}
.thumbnails__scroller::-webkit-scrollbar {
display: none;
}
.thumbnails__image {
width: 64px;
height: 64px;
object-fit: cover;
opacity: 0.3;
will-change: opacity;
transition: 250ms opacity;
cursor: pointer;
}
.thumbnails__image.active {
opacity: 1;
}
</style>
2023-02-23 16:50:18 -05:00
2022-08-06 08:45:45 +00:00
<script>
{
const carousel = document.querySelector('.carousel-thumbnails');
const scroller = document.querySelector('.thumbnails__scroller');
const thumbnails = document.querySelectorAll('.thumbnails__image');
scroller.addEventListener('click', e => {
const target = e.target;
if (target.matches('.thumbnails__image')) {
const index = [...thumbnails].indexOf(target);
carousel.goToSlide(index);
}
});
carousel.addEventListener('sl-slide-change', e => {
const slideIndex = e.detail.index;
[...thumbnails].forEach((thumb, i) => {
thumb.classList.toggle('active', i === slideIndex);
if (i === slideIndex) {
thumb.scrollIntoView({
block: 'nearest'
});
}
});
});
}
</script>
```
2023-01-08 13:36:24 +01:00
```jsx react
import { useRef } from 'react';
import { SlCarousel, SlCarouselItem, SlDivider, SlRange } from '@shoelace -style/shoelace/dist/react';
const css = `
.carousel-thumbnails {
--slide-aspect-ratio: 3 / 2;
}
.thumbnails {
display: flex;
justify-content: center;
}
.thumbnails__scroller {
display: flex;
gap: var(--sl-spacing-small);
overflow-x: auto;
scrollbar-width: none;
scroll-behavior: smooth;
scroll-padding: var(--sl-spacing-small);
}
.thumbnails__scroller::-webkit-scrollbar {
display: none;
}
.thumbnails__image {
width: 64px;
height: 64px;
object-fit: cover;
opacity: 0.3;
will-change: opacity;
transition: 250ms opacity;
cursor: pointer;
}
.thumbnails__image.active {
opacity: 1;
}
`;
const images = [
{
2023-02-23 14:39:05 -05:00
src: '/assets/examples/carousel/mountains.jpg',
2023-02-24 15:56:31 -05:00
alt: 'The sun shines on the mountains and trees (by Adam Kool on Unsplash'
2023-01-08 13:36:24 +01:00
},
{
2023-02-23 14:39:05 -05:00
src: '/assets/examples/carousel/waterfall.jpg',
2023-02-24 15:56:31 -05:00
alt: 'A waterfall in the middle of a forest (by Thomas Kelly on Unsplash'
2023-01-08 13:36:24 +01:00
},
{
2023-02-23 14:39:05 -05:00
src: '/assets/examples/carousel/sunset.jpg',
2023-02-24 15:56:31 -05:00
alt: 'The sun is setting over a lavender field (by Leonard Cotte on Unsplash'
2023-01-08 13:36:24 +01:00
},
{
2023-02-23 14:39:05 -05:00
src: '/assets/examples/carousel/field.jpg',
2023-02-24 15:56:31 -05:00
alt: 'A field of grass with the sun setting in the background (by Sapan Patel on Unsplash'
2023-01-08 13:36:24 +01:00
},
{
2023-02-23 14:39:05 -05:00
src: '/assets/examples/carousel/valley.jpg',
2023-02-24 15:56:31 -05:00
alt: 'A scenic view of a mountain with clouds rolling in (by V2osk on Unsplash'
2023-01-08 13:36:24 +01:00
}
];
const App = () => {
const carouselRef = useRef();
const thumbnailsRef = useRef();
const [currentSlide, setCurrentSlide] = useState(0);
useEffect(() => {
const thumbnails = Array.from(thumbnailsRef.current.querySelectorAll('.thumbnails__image'));
thumbnails[currentSlide]..scrollIntoView({
block: 'nearest'
});
}, [currentSlide]);
const handleThumbnailClick = (index) => {
carouselRef.current.goToSlide(index);
}
const handleSlideChange = (event) => {
const slideIndex = e.detail.index;
setCurrentSlide(slideIndex);
}
return (
<>
<SlCarousel className="carousel-thumbnails" navigation loop onSlSlideChange={handleSlideChange}>
{images.map({ src, alt }) => (
<SlCarouselItem>
<img
alt={alt}
src={src}
/>
</SlCarouselItem>
)}
</SlCarousel>
<div class="thumbnails">
<div class="thumbnails__scroller">
{images.map({ src, alt }, i) => (
<img
2023-02-24 15:56:31 -05:00
alt={`Thumbnail by ${i + 1}` }
2023-01-08 13:36:24 +01:00
className={`thumbnails__image ${i === currentSlide ? 'active' : ''}` }
onCLick={() => handleThumbnailClick(i)}
src={src}
/>
)}
</div>
</div>
<style>{css}</style>
</>
);
};
```
2022-08-06 08:45:45 +00:00
[component-metadata:sl-carousel]