Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including vue-agile-carousel with all npm packages installed. Try it out:

var vueAgileCarousel = require("vue-agile-carousel")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

vue-agile-carousel v0.4.0

Carousel component for Vue.js

vue-agile

This is a fork of vue-agile. I changed beforeChange and afterChange event names in dev branch to kebab-case and merged to master.

I needed it for a project and the author wasn't responding.


Old READEME:

Carousel component for Vue.js inspired by Slick.
More powerful with each version, touch-friendly, written in Vue and Vanilla JS (without jQuery dependency).

Demo & examples

🤝 Thanks to Maciej Wach for inventing the name, testing and motivation.


Please note that the plugin is currently in relatively early development phase. I try to keep the stability of each version and keep up with all issues, but unfortunately at this stage there may still be some bugs.


Important – update from version < 0.3

In version 0.3.0, I removed all styles that are responsible for the appearance of navigation elements (like dots color and shape, arrows position, etc.). I think most people use their own styles and default styles are completely redundant. If you want to check out these defaults styles, you can find them here.

Additionally dots and arrows have been moved from the .agile__list to the main .agile container. The layout is the same as in Slick and id should allow for better and more comfortable positioning of these elements.

Installation

yarn add vue-agile

or

npm install vue-agile

Usage

import Vue from 'vue'
import VueAgile from 'vue-agile'

Vue.use(VueAgile)
<template>
    <main>
        <agile>
            <div class="slide">
                <h3>slide 1</h3>
            </div>
            
            ...
            
            <div class="slide">
                <h3>slide n</h3>
            </div>
        </agile>
    </main>
</template>

Every first-level child of <agile> is a new slide.

Options

ParameterTypeDefaultDescription
arrowsbooleantrueEnable prev/next arrows
asNavForarray (refs)[]Set the slider to be the navigation of other one – more in „asNavFor” section
autoplaybooleanfalseEnable autoplay
autoplaySpeedinteger (ms)3000Autoplay interval in milliseconds
centerModebooleanfalseEnables active slides centering
centerPaddingstring (px/%)15%Side padding for center mode
dotsbooleantrueEnable dot indicators/pagination
fadebooleanfalseEnable fade effect
infinitebooleantrueInfinite loop sliding
initialSlideinteger0Slide to start on (numbered from zero)
mobileFirstbooleantrueEnable mobile first calculation for responsive settings
nextArrowstring (HTML/SVG)<svg>Next arrow code – more in „Arrows” section
optionsobjectnullAll settings as one object
pauseOnHoverbooleantruePause autoplay when a slide is hovered
pauseOnDotsHoverbooleanfalsePause autoplay when a dot is hovered
prevArrowstring (HTML/SVG)<svg>Prev arrow code – more in „Arrows” section
responsiveobjectnullObject containing breakpoints and settings objects
slidesToShowinteger1Number of slides to show
speedinteger (ms)300Slide animation speed in milliseconds
timingstringeaseTransition timing function
(linear/ease/ease-in/ease-out/ease-in-out)
unagilebooleanfalseDisable agile carousel

Example

<agile :arrows="false" :dots="false" :infinite="false">
    ...
</agile>

Responsive

To customize responsiveness, I recommend defining desired breakpoint and passing a settings object with the options to modify inside options.

Example

<agile :options="options">
    ...
</agile>
data () {
    return {
        options: {
            arrows: false,
            
            responsive: [
                {
                    breakpoint: 600,
                    settings: {
                        dots: false
                    }
                },
                
                {
                    breakpoint: 900,
                    settings: {
                        arrows: true,
                        dots: true,
                        infinite: false
                    }
                }
            ]
        }
    }
}

asNavFor

With this parameter you can synchronize several Agile carousels. For example, one can show thumbnails of photos and the second big picture. Changing a slide on one carousel will cause a change to another.

Example

<agile ref="agile1" asNavFor="['agile2', 'agile3']">
    ...
</agile>
 
<agile ref="agile2">
    ...
</agile>

<agile ref="agile3">
    ...
</agile>

You can also combine carousel in two ways:

<agile ref="bigImage" asNavFor="['thumbnails']">
    ...
</agile>

<agile ref="thumbnails" asNavFor="['bigImage']">
    ...
</agile>

Arrows

By default carousel contains SVG arrows. You can change them using CSS or prevArrow & nextArrow parameters.

There are two important things:

  1. You should put your SVG/HTML code in one line, without new line chars etc.
  2. You should define arrows code as variables in data().

Example

<agile :prevArrow="left" :nextArrow="right">
    ...
</agile>
export default {
    data () {
        return {
            left: '<svg x="0px" y="0px" viewBox="0 0 24 24"><path d="M16.2,21c0.3,0,0.5-0.1,0.7-0.3c0.4-0.4,0.4-1,0-1.4L9.6,12L17,4.7c0.4-0.4,0.4-1,0-1.4c-0.4-0.4-1-0.4-1.4,0L6.8,12l8.8,8.7C15.7,20.9,16,21,16.2,21z"/></svg>',
            right: '<svg x="0px" y="0px" viewBox="0 0 24 24"><path d="M7.8,21c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l7.4-7.3L7,4.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l8.8,8.7l-8.8,8.7C8.3,20.9,8,21,7.8,21z"/></svg>'
        }
    }
}

Events

NameReturned dataDescription
beforeChange{currentSlide, nextSlide}Fires before slide change
afterChange{currentSlide}Fires after slide change

Example

<agile @afterChange="newSlide($event)">
    ...
</agile>
newSlide ($event) {
    console.log($event.currentSlide)
}

Methods

NameParametersReturned dataDescription
goTo()slide number (integer), don't animate (optional boolean)Navigates to a slide by index
goToNext()Navigates to the next slide
goToPrev()Navigates to the previous slide
getCurrentSlide()slide number (integer)Returns the current slide index (numbered from zero)

Example

<agile ref="carousel">
    ...
</agile>    
// Go to slide 3 without animation
this.$refs.carousel.goTo(3, false)

// Get current slide number
let currentSlide = this.$refs.carousel.getCurrentSlide()

v-if & v-show

If you have dynamically loaded slides, use v-if to show carousel when slides will be ready. Using v-if is also recommended in other situations if you want to hide/show the slideshow.

If for some reason you need to use v-show, it's also possible, but you have to use an additional parameter show with the same value as for the v-show.

Example

<button @click="toggleAgile()">Toggle carousel</button>

<agile v-show="isActive" :show="isActive">
    ...
</agile>
toggleAgile () {
    this.isActive = !this.isActive
}

SSR Support

The component uses browser specific attributes (like window and document). It is necessary, so probably the only option is to run vue-agile only on the client-side. It was tested on nuxt v1.0.0-rc7 and works fine.

Example

// plugins/vue-agile.js

import Vue from 'vue'
import VueAgile from 'vue-agile'

Vue.use(VueAgile)
// nuxt.config.js

module.exports = {
    plugins: [
        { src: '~/plugins/vue-agile', ssr: false }
    ]
}
<no-ssr placeholder="Loading...">
    <agile>
        ... 
    </agile>
</no-ssr>

PS. If you know a better way to work the component with SSR please, let me know about it.

Contributing

# install dependencies
yarn install
 
# serve with hot reload at localhost:8080
yarn run dev
 
# create UMD bundle.
yarn run bundle

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free