-
Install the npm package
npm install @samwood/vue-carousel
-
Import VueCarousel into Vue component script
// my-component.vue <template> <MyComponent>...</MyComponent> </template> <script> import VueCarousel from '@samwood/vue-carousel'; export default { name: 'MyComponent', components: { VueCarousel } } </script>
-
Add VueCarousel to Vue component template
To populate the slides, you will need to add
<template>
for each slide with av-slot[0]
starting at base 0. Eachv-slot
should increment by 1 for every slide.// my-component.vue <template> <MyComponent> <VueCarousel> <template v-slot[0]> <MyChildComponent /> </template> <template v-slot[1]> <MyChildComponent /> </template> <template v-slot[3]> <MyChildComponent /> </template> </VueCarousel> </MyComponent> </template> <script> ... </script>
Optionally, you can dynamically change the v-slot to avoid repeated code.
// my-component.vue <template> <MyComponent> <VueCarousel> <template v-for="n in slides" v-slot[n-1]> <MyChildComponent :key="`slide-${n-1}`" /> </template> </VueCarousel> </MyComponent> </template> <script> ... </script>
Vue carousel takes a config
prop - an object contain various keys to change the functionality and appearance of the component.
<vue-carousel
:config="{
slidesVisible: {...},
staticBreakpoint: "md",
loop: true
}"
>
...
</vue-carousel>
Key | Type | Default Value | Description |
---|---|---|---|
autoplay | Boolean |
|
Determines if the carousel will slide automatically based on `autoplayInterval`. |
autoplayHoverPause | Boolean |
|
Determines if the carousel should stop auto sliding when the mouse hovers the carousel. |
autoplayInterval | Number |
|
Changes the timing for auto slide in milliseconds. |
breakpoints | Object |
|
Customise the breakpoints. All breakpoints must be defined and values must be valid or default will be used. |
center | Boolean |
|
Aligns the current slide in the center of the carousel instead of to the left-side. Caveat - `center` will be disabled if `group` is `true`. |
controls | Object |
|
Change the contents and styles of the previous/next controls and pagination for the carousel. HTML can be passed in a String to `previous` and `next`. Styles must be passed as an object. Example:
|
debug | Boolean |
|
Allows warnings and caught-errors in console if enabled. |
group | Boolean |
|
Group all items in current view to move all outside of view and show all new items when the carousel is dragged or the pagination or control buttons are clicked. By default, items are treated individually and will move one at a time. Caveat - if both `group` and `loop` are set to true, ensure there is no remainder of slides when dividing total slide-count by the value of `slidesVisible` at the current breakpoint. |
loop | Boolean |
|
Determines if the carousel should loop infintely. If false, carousel will only animate between first and last slide. Caveat - loop will be disabled if `group` is `true` but slide-count divided by the `slideVisible` value at the current breakpoint leaves a remainder. |
mouseDrag | Boolean |
|
Determines if the carousel slides can be dragged using the mouse when holding left click. Note - the slides can be dragged on touch devices still even when mouseDrag is set to false. This option works better when slides are not populated with links. |
showEmptySpace | Boolean |
|
Determines if the carousel can be swiped until only the last slide is visible on the left side of the screen. If false, the carousel will not be slideable once the last slide is visible on screen. For pagination - If true, a pagination button will be used for each slide. If false, pagination buttons will only show for the first slide rendered up to the first slide visible where the last slide is on screen (paginationCount = totalSlides - visibleSlidesOnScreen + 1). This is to prevent sliding past the last slide. Note - this option has no effect if loop is true. |
slidePadding | Object |
|
Change the padding between slides for specific breakpoints. If a unit of measurement is not included in the value, it will be assumed to be a pixel value. Example:
|
slidesVisible | Object |
|
Change the number of slides visible at specific breakpoints. The number can be a whole number or decimal. If a breakpoint is null, the carousel will use the value from a previous breakpoint. |
startingSlide | Number |
|
The index of the slide to start on when the carousel renders. If the index is invalid, the default will be used. |
staticBreakpoint | String |
|
Removes the carousel functionality and animation at the specified breakpoint and shows all items on screen. Optional Values:
|
touchDrag | Boolean |
|
Determines if the user can drag carousel slides with touch input such as on mobile and tablets or with touch screen monitors. This is set to true by default as touch drag is better UX on touch devices compared to button presses. |
transitionDuration | Number |
|
The CSS transition-duration property that controls how long it takes to animate between slides. |
transitionTimingFunction | String |
|
The CSS transition-timing-function property that controls the animation timing function between slides. |
Event | type | Description |
---|---|---|
slide-change | number | Emits the new slide number on slide-change. |
Although the controls have options to pass styling through a JS object, this isn't always ideal, especially when you want to use your own CSS variables and mixins. You will find below a list of CSS classes that you can target to override existing styles.
.v-carousel
If you set a breakpoint to become static, you may want to change the layout of the slides. You can do this by targetting the following class:
.v-carousel--static
.v-carousel__controls__btn
.v-carousel__controls__btn--prev
.v-carousel__controls__btn--next
.v-carousel__controls__btn--disabled
.v-carousel-pagination
.v-carousel-pagination__list // ul/ol element containing li elements
.v-carousel-pagination__li // li element wrapping the button
.v-carousel-pagination__btn
.v-carousel-pagination__btn--active
.v-carousel-pagination__btn--number
.v-carousel__slide
Note: Ensure that you do not apply any attributes to slide slots within the carousel that should not be duplicated such as id
as if you set loop
to true
, the content in these slides will be duplicated and will cause issues.
The carousel uses base-0 incrementing numbers for slot names.
v-for="n in 10"
will produce numbers 1-10 for n
. You will need to use v-slot:[n-1]
to include the first slides content.
If you are using scoped styles, ensure that you are using ::v-deep
before targetting the carousel class.
Example
<style lang="scss scoped>
.my-component {
& ::v-deep .v-carousel--static {
display: grid;
}
}
</style>
The carousel uses an Intersection Observer to remove the autoplay-interval when the carousel is not visible on screen.
This has been done to improve efficiency of the browser so that the carousel is not carrying out performance-expensive animations that may interfer with the user's experience.
Autoplay will be enabled again when the carousel is visible again.
If config.group
is set to true
, ensure that the total slide-count can be divided by your config.slidesVisible
value at the current breakpoint without remainder. e.g
slide-count (10) % config.slidesVisible.xs (2) = 0
✅
slide-count (10) % config.slidesVisible.sm (3) = 1
❌
If there is a remainder, loop
will be disabled because there will not be enough items to fill out the last group of slides.