I need a 0 CLS slider for nuxt

Hi, I'm working on a e-commerce website and i need a slider for hero images. I'm currently trying to use @splidejs/vue but its resulting a bad CLS score. I also tested out with nuxt-splide, same thing happens. Can anyone help me about this ? If you encounter such an issue please let me know
5 Replies
kapa.ai
kapa.ai3w ago
Hi @mentalrob. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To address your need for a zero CLS (Cumulative Layout Shift) slider in Nuxt for your e-commerce website's hero images, I can suggest using the UCarousel component from the Nuxt UI library. This component is designed to work well with Nuxt and can help minimize CLS issues. Here's how you can implement it: 1. First, make sure you have the Nuxt UI library installed in your project. 2. Use the UCarousel component in your template. Here's an example of how to implement it with autoplay:
<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'>
]

const carouselRef = ref()

onMounted(() => {
setInterval(() => {
if (!carouselRef.value) return

if (carouselRef.value.page === carouselRef.value.pages) {
return carouselRef.value.select(0)
}

carouselRef.value.next()
}, 3000)
})
</script>

<template>
<UCarousel
ref="carouselRef"
v-slot="{ item }"
:items="items"
:ui="{ item: 'basis-full' }"
class="rounded-lg overflow-hidden"
indicators
>
<img :src="item" class="w-full" draggable="false">
</UCarousel>
</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'>
]

const carouselRef = ref()

onMounted(() => {
setInterval(() => {
if (!carouselRef.value) return

if (carouselRef.value.page === carouselRef.value.pages) {
return carouselRef.value.select(0)
}

carouselRef.value.next()
}, 3000)
})
</script>

<template>
<UCarousel
ref="carouselRef"
v-slot="{ item }"
:items="items"
:ui="{ item: 'basis-full' }"
class="rounded-lg overflow-hidden"
indicators
>
<img :src="item" class="w-full" draggable="false">
</UCarousel>
</template>
This implementation uses the UCarousel component with autoplay functionality. It's designed to work well with Nuxt and should help minimize CLS issues. To further optimize for CLS, consider the following tips: 1. Preload your hero images to ensure they're available as soon as possible. 2. Use fixed dimensions for your slider container to reserve space before the images load. 3. Use the loading="lazy" attribute on images that are not immediately visible. For more advanced optimizations, you can refer to the Nuxt documentation on styling and advanced optimizations, which provides tips on improving Largest Contentful Paint (LCP) that can also help with CLS. If you need more control over the slider behavior or want to implement additional features, you can explore the full API of the UCarousel component in the Nuxt UI documentation. Remember, the key to minimizing CLS is to ensure that the layout is stable as soon as possible, which this solution aims to achieve. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@214309928494432257> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
mentalrob
mentalrobOP3w ago
@kapa.ai nuxt ui's carousel is not good enough.
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand that you find Nuxt UI's carousel component not suitable for your needs. Since you're looking for alternatives, let me suggest a few options that might work better for your e-commerce website's hero images while maintaining good CLS (Cumulative Layout Shift) scores: 1. Vue 3 Carousel: This is a lightweight and customizable carousel component for Vue 3. It's not specifically designed for Nuxt, but it should work well with Nuxt 3 projects. You can find more information and implementation details in their documentation. 2. Swiper: While you mentioned issues with @splidejs/vue, Swiper is another popular option that can be used with Vue and Nuxt. It offers a wide range of features and customization options. You can find more information on how to integrate it with Nuxt in their documentation. 3. Custom implementation: If you need very specific functionality or performance, you might consider creating a custom carousel component tailored to your needs. This would give you full control over the implementation and allow you to optimize for CLS specifically. When implementing any of these solutions, keep in mind the following tips to minimize CLS: - Pre-define the dimensions of your carousel container - Preload critical images - Use appropriate image sizes and formats - Implement lazy loading for non-critical images If you decide to go with a custom implementation or need more specific advice on optimizing your chosen solution for CLS, please provide more details about your exact requirements and the issues you're facing. This will help in providing more targeted suggestions. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@214309928494432257> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?