Dynamic Carousel Page
How to change the page of a nuxtui carousel using another SelectMenu
select()select()carouselRefselect(page)select(page)next()next()prev()prev()pagepagepagespagescarouselRef.value.select is not a functionselectselectselectselectselect (page) Go to a specific page.scrollTo()<template>
<div>
<UButton @click="goToPage(2)">Go to page 3</UButton>
<UCarousel ref="carouselRef" ... />
</div>
</template>
<script setup>
const carouselRef = ref()
function goToPage(page) {
console.log('Carousel ref:', carouselRef.value)
if (carouselRef.value) {
try {
carouselRef.value.select(page)
} catch (e) {
console.error('Error:', e)
// Log available methods
console.log('Available methods:', Object.keys(carouselRef.value))
}
}
}
</script><script setup>
const carouselRef = ref()
// Access the Embla API through the ref
onMounted(() => {
nextTick(() => {
if (carouselRef.value && carouselRef.value.emblaApi) {
// Now you can use emblaApi methods
carouselRef.value.emblaApi.scrollTo(index)
// For instant navigation without animation, you can use:
carouselRef.value.emblaApi.scrollTo(index, true) // The second parameter (true) enables the "jump" option
}
})
})
</script>
<template>
<UCarousel ref="carouselRef" :items="items" ... />
</template><script setup lang="ts">
const items = [
'<https://picsum.photos/1920/1080?random=1',>
'<https://picsum.photos/1920/1080?random=2',>
'<https://picsum.photos/1920/1080?random=3',>
'<https://picsum.photos/1920/1080?random=4',>
'<https://picsum.photos/1920/1080?random=5',>
'<https://picsum.photos/1920/1080?random=6'>
]
// Create options for the select menu
const options = items.map((_, index) => ({
label: `Slide ${index + 1}`,
value: index
}))
const selectedPage = ref(0)
const carouselRef = ref()
// Watch for changes in the select menu
watch(selectedPage, (newPage) => {
if (carouselRef.value) {
carouselRef.value.select(newPage)
}
})
</script>
<template>
<div>
<!-- Select menu to control the carousel -->
<USelectMenu
v-model="selectedPage"
:options="options"
placeholder="Select a slide"
/>
<!-- Carousel controlled by the select menu -->
<UCarousel
ref="carouselRef"
v-slot="{ item }"
:items="items"
:ui="{ item: 'basis-full' }"
class="rounded-lg overflow-hidden mt-4"
indicators
>
<img :src="item" class="w-full" draggable="false">
</UCarousel>
</div>
</template>