vue3-carousel

repository·master·Indexed 21 days ago

https://github.com/ismail9k/vue3-carousel

A modern, lightweight carousel component for Vue 3 (version 0.17.0). It supports responsive breakpoints, infinite scrolling, touch/mouse dragging, and accessibility (A11y). The library provides core components like Carousel and Slide, along with Navigation and Pagination addons, and offers a dedicated vue3-carousel-nuxt module for Nuxt users.

Tokens
16.3K
Snippets
59
Records
75
Agent score
75%

What's inside vue3-carousel

  1. Use Carousel Slots for Slides and Addons

    master

    The Carousel component utilizes specific slots to organize its content:

    • Default/Slides Slot: Used to render the carousel items. You can either place Slide components directly in the default slot or wrap them in a <template #slides> block.
    • Addons Slot: Used to inject UI components like <Navigation /> and <Pagination /> via the <template #addons> block.
    <template>
      <Carousel>
        <template #slides>
          <Slide v-for="slide in 10" :key="slide">
            ...
          </Slide>
        </template>
    
        <template #addons>
          <Navigation />
          <Pagination />
        </template>
      </Carousel>
    </template>
  2. Use the Navigation component

    master

    The Navigation component provides Previous and Next buttons for slide navigation. It should be placed within the #addons slot of the Carousel component. It automatically handles RTL support, vertical navigation, and disables itself when the carousel reaches its bounds.

    <script setup>
    import { Navigation as CarouselNavigation } from 'vue3-carousel'
    </script>
    
    <template>
      <Carousel>
        <Slide v-for="slide in 10" :key="slide">
          <!-- slide content -->
        </Slide>
        
        <template #addons>
          <CarouselNavigation />
        </template>
      </Carousel>
    </template>
  3. Use the Slide component

    master

    The Slide component is the fundamental building block used inside a Carousel to represent individual slides. It manages visibility, positioning, and state transitions. When rendering multiple slides using v-for, always provide a unique :key to ensure correct rendering and transitions.

    <template>
      <Carousel>
        <Slide v-for="slide in 10" :key="slide">
          <div class="carousel__item">
            {{ slide }}
          </div>
        </Slide>
      </Carousel>
    </template>
  4. Customize visual appearance and transitions

    master

    Customize the look and feel using gap, snapAlign, slideEffect, and transitionEasing. transitionEasing accepts any valid CSS timing function.

    <template>
      <!-- Custom gap, alignment, and fade effect -->
      <Carousel 
        :gap="20"
        snap-align="start"
        slide-effect="fade"
        :transition="500"
      >
        <!-- Slides -->
      </Carousel>
    
      <!-- Custom transition with easing -->
      <Carousel
        :transition="600"
        transition-easing="cubic-bezier(0.4, 0, 0.2, 1)"
      >
        <!-- Slides -->
      </Carousel>
    </template>
  5. Configure navigation and autoplay

    master

    Control how users interact with the carousel using autoplay, mouseDrag, touchDrag, and mouseWheel. You can also use pauseAutoplayOnHover to improve user experience.

    <template>
      <Carousel 
        :autoplay="3000"
        :pause-autoplay-on-hover="true"
        :mouse-drag="true"
        :touch-drag="true"
        :mouse-wheel="{ threshold: 20 }"
      >
        <!-- Slides -->
      </Carousel>
    </template>
  6. Configure item display and direction

    master

    Use itemsToShow to control visibility and itemsToScroll to define navigation steps. For vertical carousels, you must set the dir prop and provide a height.

    <template>
      <!-- Basic setup with 3 items visible -->
      <Carousel :items-to-show="3" :wrap-around="true" :transition="500">
        <Slide v-for="slide in 10" :key="slide">
          Slide {{ slide }}
        </Slide>
      </Carousel>
    
      <!-- Vertical orientation requires a height -->
      <Carousel dir="ttb" :height="300">
        <!-- Slides -->
      </Carousel>
    </template>
  7. Use the Pagination component

    master

    The Pagination component provides interactive indicators that allow users to navigate directly to specific slides. To use it, import Pagination from vue3-carousel and place it within the #addons slot of the Carousel component.

    <script setup>
    import { Carousel, Slide, Pagination as CarouselPagination } from 'vue3-carousel'
    </script>
    
    <template>
      <Carousel>
        <Slide v-for="slide in 10" :key="slide">
          <!-- slide content -->
        </Slide>
        
        <template #addons>
          <CarouselPagination />
        </template>
      </Carousel>
    </template>
  8. Access Carousel data via template refs

    master

    To access the internal state and data of a carousel, assign a ref to the <Carousel> component in your template. You can then access the carousel's properties through the .data property of that ref instance.

    <Carousel ref="myCarousel"> ... </Carousel>
    import { ref } from 'vue';
    const myCarousel = ref(null);
    
    // Access data properties via the ref
    // Note: In the actual implementation, you access the exposed properties
    if (myCarousel.value.currentSlide === 10) {
      // Do your magic here
    }
  9. Basic Usage of the Carousel Component

    master

    The Carousel component is the primary container for managing slides and navigation. To use it, wrap Slide components within the Carousel. You can also include addon components like Navigation and Pagination using the #addons slot.

    <template>
      <Carousel>
        <Slide v-for="slide in 10" :key="slide">
          <div class="carousel__item">{{ slide }}</div>
        </Slide>
        
        <template #addons>
          <Navigation />
          <Pagination />
        </template>
      </Carousel>
    </template>