Bearbeiten
Verwenden Sie die Karussell-Komponente, um eine Liste von Elementen in einem Karussell anzuzeigen.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
loop
arrows
:autoplay="{ delay: 2000 }"
wheel-gestures
:prev="{ variant: 'solid' }"
:next="{ variant: 'solid' }"
:items="items"
:ui="{
item: 'basis-1/3 ps-0',
prev: 'sm:start-8',
next: 'sm:end-8',
container: 'ms-0'
}"
>
<img :src="item" width="320" height="320">
</UCarousel>
</template>
Einträge
Verwenden Sie die items prop als Array und rendern Sie jedes Element mit dem Standard-Slot:
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel v-slot="{ item }" :items="items" class="w-full max-w-xs mx-auto">
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Sie können auch ein Array von Objekten mit den folgenden Eigenschaften übergeben:
class?: any(englisch)ui?: { item?: ClassNameValue }(nicht vorhanden)
Sie können steuern, wie viele Elemente sichtbar sind, indem Sie die Hilfsklassen basis/width auf den item verwenden:
<script setup lang="ts">
const items = [
'https://picsum.photos/468/468?random=1',
'https://picsum.photos/468/468?random=2',
'https://picsum.photos/468/468?random=3',
'https://picsum.photos/468/468?random=4',
'https://picsum.photos/468/468?random=5',
'https://picsum.photos/468/468?random=6'
]
</script>
<template>
<UCarousel v-slot="{ item }" :items="items" :ui="{ item: 'basis-1/3' }">
<img :src="item" width="234" height="234" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Ausrichtung
Verwenden Sie die orientation-Prop, um die Ausrichtung des Progress. Defaults auf horizontal zu ändern.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
orientation="vertical"
:items="items"
:ui="{ container: 'h-[336px]' }"
class="w-full max-w-xs mx-auto"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
height auf dem Container in vertikaler Ausrichtung angeben.Arrows Bearbeiten
Verwenden Sie die arrows-Prop, um die Tasten prev und next anzuzeigen.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel v-slot="{ item }" arrows :items="items" class="w-full max-w-xs mx-auto">
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Prev/Next (Deutsche Übersetzung)
Verwenden Sie die prev und next props, um die prev und next Tasten mit beliebigen Button props anzupassen.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
arrows
:prev="{ color: 'primary' }"
:next="{ variant: 'solid' }"
:items="items"
class="w-full max-w-xs mx-auto"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Prev/Next Icons (Deutsche Ausgabe)
Verwenden Sie die Props prev-icon und next-icon, um die Schaltflächen Icon anzupassen.
<script setup lang="ts">
defineProps<{
prevIcon?: string
nextIcon?: string
}>()
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
arrows
:prev-icon="prevIcon"
:next-icon="nextIcon"
:items="items"
class="w-full max-w-xs mx-auto"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
app.config.ts unter der Taste ui.icons.arrowLeft/ui.icons.arrowRight anpassen.vite.config.ts unter ui.icons.arrowLeft/ui.icons.arrowRight Schlüssel anpassen.Dots Bearbeiten
Verwenden Sie die dots-Prop, um eine Liste von Punkten anzuzeigen, um zu einer bestimmten Folie zu scrollen.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel v-slot="{ item }" dots :items="items" class="w-full max-w-xs mx-auto">
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Die Anzahl der Punkte basiert auf der Anzahl der Folien, die in der Ansicht angezeigt werden:
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel v-slot="{ item }" dots :items="items" :ui="{ item: 'basis-1/3' }">
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Plug-Ins für
Die Carousel Komponente implementiert die offiziellen Embla Carousel Plugins.
Autoplay (nicht)
Dieses Plugin wird verwendet, um Embla Carousel mit der autoplay-Funktionalität zu erweitern.
Verwenden Sie die autoplay-Prop als Boolean oder Objekt, um das Autoplay-Plugin zu konfigurieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/468/468?random=1',
'https://picsum.photos/468/468?random=2',
'https://picsum.photos/468/468?random=3',
'https://picsum.photos/468/468?random=4',
'https://picsum.photos/468/468?random=5',
'https://picsum.photos/468/468?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
loop
arrows
dots
:autoplay="{ delay: 2000 }"
:items="items"
:ui="{ item: 'basis-1/3' }"
>
<img :src="item" width="234" height="234" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
loop prop für ein unendliches Karussell.Auto Scroll (englisch)
Dieses Plugin wird verwendet, um Embla Carousel mit auto scroll Funktionalität zu erweitern.
Verwenden Sie die auto-scroll-Prop als Boolean oder Objekt, um das Auto Scroll-Plugin zu konfigurieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/468/468?random=1',
'https://picsum.photos/468/468?random=2',
'https://picsum.photos/468/468?random=3',
'https://picsum.photos/468/468?random=4',
'https://picsum.photos/468/468?random=5',
'https://picsum.photos/468/468?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
loop
dots
arrows
auto-scroll
:items="items"
:ui="{ item: 'basis-1/3' }"
>
<img :src="item" width="234" height="234" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
loop prop für ein unendliches Karussell.Auto Height (englisch)
Dieses Plugin wird verwendet, um Embla Carousel mit auto height Funktionalität zu erweitern. Es ändert die Höhe des Karussellcontainers, um die Höhe der höchsten Folie in der Ansicht zu passen.
Verwenden Sie die auto-height-Prop als Boolean oder Objekt, um das Auto Height-Plugin zu konfigurieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/320?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/320?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/320?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
auto-height
arrows
dots
:items="items"
:ui="{
container: 'transition-[height]',
controls: 'absolute -top-8 inset-x-12',
dots: '-top-7',
dot: 'w-6 h-1'
}"
class="w-full max-w-xs mx-auto"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
transition-[height] hinzu, um die Höhenänderung zu animieren.Class-Namen
Class Names ist ein class name toggle utility plugin für Embla Carousel, mit dem Sie das Umschalten von Klassennamen auf Ihrem Karussell automatisieren können.
Verwenden Sie die class-names-Prop als Boolean oder als Objekt, um das Class-Names-Plugin zu konfigurieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/528/528?random=1',
'https://picsum.photos/528/528?random=2',
'https://picsum.photos/528/528?random=3',
'https://picsum.photos/528/528?random=4',
'https://picsum.photos/528/528?random=5',
'https://picsum.photos/528/528?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
class-names
arrows
:items="items"
:ui="{
item: 'basis-[70%] transition-opacity [&:not(.is-snapped)]:opacity-10'
}"
class="mx-auto max-w-sm"
>
<img :src="item" width="264" height="264" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
transition-opacity [&:not(.is-snapped)]:opacity-10-Klassen auf der item hinzu, um die Deckkraftänderung zu animieren.Fade (englisch)
Dieses Plugin wird verwendet, um die Embla Carousel Scroll-Funktion durch fade transitions zu ersetzen.
Verwenden Sie die fade-Prop als Boolean oder Objekt, um das Fade-Plugin zu konfigurieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
fade
arrows
dots
:items="items"
class="w-full max-w-xs mx-auto"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Wheel Gestures Bearbeiten
Dieses Plugin wird verwendet, um Embla Carousel mit der Fähigkeit zu erweitern, use die Maus/Trackpad wheel, um das Karussell zu navigieren.
Verwenden Sie die wheel-gestures-Prop als Boolean oder Objekt, um das Wheel Gestures plugin zu konfigurieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/468/468?random=1',
'https://picsum.photos/468/468?random=2',
'https://picsum.photos/468/468?random=3',
'https://picsum.photos/468/468?random=4',
'https://picsum.photos/468/468?random=5',
'https://picsum.photos/468/468?random=6'
]
</script>
<template>
<UCarousel
v-slot="{ item }"
loop
wheel-gestures
:items="items"
:ui="{ item: 'basis-1/3' }"
>
<img :src="item" width="234" height="234" class="rounded-lg" loading="lazy">
</UCarousel>
</template>
Beispiele
With Thumbnails (Deutsche Ausgabe)
Sie können die scrollTo-Methode auf emblaApi verwenden, um Karussell unter den Miniaturansichten anzuzeigen, die zu einer bestimmten Folie navigieren.
<script setup lang="ts">
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
const carousel = useTemplateRef('carousel')
const activeIndex = ref(0)
function onClickPrev() {
activeIndex.value--
}
function onClickNext() {
activeIndex.value++
}
function onSelect(index: number) {
activeIndex.value = index
}
function select(index: number) {
activeIndex.value = index
carousel.value?.emblaApi?.scrollTo(index)
}
</script>
<template>
<div class="flex-1 w-full">
<UCarousel
ref="carousel"
v-slot="{ item }"
arrows
:items="items"
:prev="{ onClick: onClickPrev }"
:next="{ onClick: onClickNext }"
class="w-full max-w-xs mx-auto"
@select="onSelect"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
<div class="flex gap-1 justify-between pt-4 max-w-xs mx-auto">
<div
v-for="(item, index) in items"
:key="index"
class="size-11 opacity-25 hover:opacity-100 transition-opacity"
:class="{ 'opacity-100': activeIndex === index }"
@click="select(index)"
>
<img :src="item" width="44" height="44" class="rounded-lg" loading="lazy">
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, useTemplateRef } from 'vue'
const items = [
'https://picsum.photos/640/640?random=1',
'https://picsum.photos/640/640?random=2',
'https://picsum.photos/640/640?random=3',
'https://picsum.photos/640/640?random=4',
'https://picsum.photos/640/640?random=5',
'https://picsum.photos/640/640?random=6'
]
const carousel = useTemplateRef('carousel')
const activeIndex = ref(0)
function onClickPrev() {
activeIndex.value--
}
function onClickNext() {
activeIndex.value++
}
function onSelect(index: number) {
activeIndex.value = index
}
function select(index: number) {
activeIndex.value = index
carousel.value?.emblaApi?.scrollTo(index)
}
</script>
<template>
<div class="flex-1 w-full">
<UCarousel
ref="carousel"
v-slot="{ item }"
arrows
:items="items"
:prev="{ onClick: onClickPrev }"
:next="{ onClick: onClickNext }"
class="w-full max-w-xs mx-auto"
@select="onSelect"
>
<img :src="item" width="320" height="320" class="rounded-lg" loading="lazy">
</UCarousel>
<div class="flex gap-1 justify-between pt-4 max-w-xs mx-auto">
<div
v-for="(item, index) in items"
:key="index"
class="size-11 opacity-25 hover:opacity-100 transition-opacity"
:class="{ 'opacity-100': activeIndex === index }"
@click="select(index)"
>
<img :src="item" width="44" height="44" class="rounded-lg" loading="lazy">
</div>
</div>
</div>
</template>
API
Props Bearbeiten
| Prop | Default | Type |
|---|---|---|
as | 'div' | anyThe element or component this component should render as. |
prev | { size: 'md', color: 'neutral', variant: 'link' } | Omit<ButtonProps, LinkPropsKeys>Configure the prev button when arrows are enabled. |
prevIcon | appConfig.ui.icons.arrowLeft | anyThe icon displayed in the prev button. |
next | { size: 'md', color: 'neutral', variant: 'link' } | Omit<ButtonProps, LinkPropsKeys>Configure the next button when arrows are enabled. |
nextIcon | appConfig.ui.icons.arrowRight | anyThe icon displayed in the next button. |
arrows | false | boolean Display prev and next buttons to scroll the carousel. |
dots | false | boolean Display dots to scroll to a specific slide. |
orientation | 'horizontal' | "vertical" | "horizontal"The orientation of the carousel. |
items | T[] | |
autoplay | false | boolean | Partial<CreateOptionsType<OptionsType>> Enable Autoplay plugin |
autoScroll | false | boolean | Partial<CreateOptionsType<OptionsType>> Enable Auto Scroll plugin |
autoHeight | false | boolean | Partial<CreateOptionsType<{ active: boolean; breakpoints: { [key: string]: Omit<Partial<any>, "breakpoints">; }; }>> Enable Auto Height plugin |
classNames | false | boolean | Partial<CreateOptionsType<OptionsType>> Enable Class Names plugin |
fade | false | boolean | Partial<CreateOptionsType<{ active: boolean; breakpoints: { [key: string]: Omit<Partial<any>, "breakpoints">; }; }>> Enable Fade plugin |
wheelGestures | false | boolean | WheelGesturesPluginOptions Enable Wheel Gestures plugin |
align | 'center' | "start" | "center" | "end" | (viewSize: number, snapSize: number, index: number): number |
containScroll | 'trimSnaps' | false | "trimSnaps" | "keepSnaps" |
slidesToScroll | 1 | number | "auto" |
dragFree | false | boolean |
dragThreshold | 10 | number |
inViewThreshold | 0 | number | number[] |
loop | false | boolean |
skipSnaps | false | boolean |
duration | 25 | number |
startIndex | 0 | number |
watchDrag | true | false | true | (emblaApi: EmblaCarouselType, evt: PointerEventType): boolean | void |
watchResize | true | false | true | (emblaApi: EmblaCarouselType, entries: ResizeObserverEntry[]): boolean | void |
watchSlides | true | false | true | (emblaApi: EmblaCarouselType, mutations: MutationRecord[]): boolean | void |
watchFocus | true | false | true | (emblaApi: EmblaCarouselType, evt: FocusEvent): boolean | void |
active | true | boolean |
breakpoints | {} | { [key: string]: Omit<Partial<CreateOptionsType<{ align: AlignmentOptionType; axis: AxisOptionType; container: string | HTMLElement | null; slides: string | HTMLElement[] | NodeListOf<HTMLElement> | null; containScroll: ScrollContainOptionType; direction: AxisDirectionOptionType; slidesToScroll: SlidesToScrollOptionType; dragFree: boolean; dragThreshold: number; inViewThreshold: number | number[] | undefined; loop: boolean; skipSnaps: boolean; duration: number; startIndex: number; watchDrag: DragHandlerOptionType; watchResize: ResizeHandlerOptionType; watchSlides: SlidesHandlerOptionType; watchFocus: FocusHandlerOptionType; }>>, "breakpoints">; } |
ui | { root?: SlotClass; viewport?: SlotClass; container?: SlotClass; item?: SlotClass; controls?: SlotClass; arrows?: SlotClass; prev?: SlotClass; next?: SlotClass; dots?: SlotClass; dot?: SlotClass; } |
Slots Bearbeiten
| Slot | Type |
|---|---|
default | { item: T; index: number; } |
Emits (englisch)
| Event | Type |
|---|---|
select | [selectedIndex: number] |
Expose (englisch)
Sie können auf die typisierte Komponenteninstanz mit useTemplateRef zugreifen.
<script setup lang="ts">
const carousel = useTemplateRef('carousel')
</script>
<template>
<UCarousel ref="carousel" />
</template>
Dies gibt Ihnen Zugang zu den folgenden:
| Vorname | Typ |
|---|---|
emblaRef (nicht) | Ref<HTMLElement | null> Bearbeiten |
emblaApi (englisch) | 218xRef<EmblaCarouselType | null>](https://www.embla-carousel.com/docs/v8/api/methods#typescript) |
Theme Bearbeiten
export default defineAppConfig({
ui: {
carousel: {
slots: {
root: 'relative focus:outline-none',
viewport: 'overflow-hidden',
container: 'flex items-start',
item: 'min-w-0 shrink-0 basis-full',
controls: '',
arrows: '',
prev: 'absolute rounded-full',
next: 'absolute rounded-full',
dots: 'absolute inset-x-0 -bottom-7 flex flex-wrap items-center justify-center gap-3',
dot: [
'cursor-pointer size-3 bg-accented rounded-full outline-inverted/25 focus-visible:outline-3',
'transition'
]
},
variants: {
orientation: {
vertical: {
container: 'flex-col -mt-4',
item: 'pt-4',
prev: 'top-4 sm:-top-12 left-1/2 -translate-x-1/2 rotate-90 rtl:-rotate-90',
next: 'bottom-4 sm:-bottom-12 left-1/2 -translate-x-1/2 rotate-90 rtl:-rotate-90'
},
horizontal: {
container: 'flex-row -ms-4',
item: 'ps-4',
prev: 'start-4 sm:-start-12 top-1/2 -translate-y-1/2',
next: 'end-4 sm:-end-12 top-1/2 -translate-y-1/2'
}
},
active: {
true: {
dot: 'data-[state=active]:bg-inverted'
}
}
}
}
}
})import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
carousel: {
slots: {
root: 'relative focus:outline-none',
viewport: 'overflow-hidden',
container: 'flex items-start',
item: 'min-w-0 shrink-0 basis-full',
controls: '',
arrows: '',
prev: 'absolute rounded-full',
next: 'absolute rounded-full',
dots: 'absolute inset-x-0 -bottom-7 flex flex-wrap items-center justify-center gap-3',
dot: [
'cursor-pointer size-3 bg-accented rounded-full outline-inverted/25 focus-visible:outline-3',
'transition'
]
},
variants: {
orientation: {
vertical: {
container: 'flex-col -mt-4',
item: 'pt-4',
prev: 'top-4 sm:-top-12 left-1/2 -translate-x-1/2 rotate-90 rtl:-rotate-90',
next: 'bottom-4 sm:-bottom-12 left-1/2 -translate-x-1/2 rotate-90 rtl:-rotate-90'
},
horizontal: {
container: 'flex-row -ms-4',
item: 'ps-4',
prev: 'start-4 sm:-start-12 top-1/2 -translate-y-1/2',
next: 'end-4 sm:-end-12 top-1/2 -translate-y-1/2'
}
},
active: {
true: {
dot: 'data-[state=active]:bg-inverted'
}
}
}
}
}
})
]
})