Building an Image Slider with Vue.js

Building an Image Slider with Vue.js

Vue.js is one of the most popular progressive JavaScript frameworks and has many benefits compared with other frameworks. This tutorial will help you in building a Vue image slider component from scratch without using additional 3rd party libraries

Project Setup

If you haven’t do so already, defore creating a new Vue.js project you should install the Vue CLI globally on your machine by doing the following:

$ npm install -g @vue/cli
# OR
$ yarn global add @vue/cli

Create a new project with the Vue CLI:

$ vue create vue-image-slider
$ cd vue-image-slider

Then run the serve script to start a local server:

$ npm run serve 

Our Vue.js project is now running successfully.

Creating the Slider Component

You may notice that there is a HelloWorld component generated automatically. You can either rename that component to Slider and make changes inside it or remove it and create a new Slider component. However, in both cases be sure to import the right component inside App.vue.

Let’s create a new Slider component and add the following functionalities:

export default {
  name: "Slider",
  data() {
    return {
      images: [
      timer: null,
      currentIndex: 0

  mounted: function() {

  methods: {
    startSlide: function() {
      this.timer = setInterval(, 4000);

    next: function() {
      this.currentIndex += 1;
    prev: function() {
      this.currentIndex -= 1;

  computed: {
    currentImg: function() {
      return this.images[Math.abs(this.currentIndex) % this.images.length];

Let’s look what we did here:

  • We got an array of image URLs from Pixabay.
  • Set timer to null and set currentIndex to 0 for showing the first image.
  • Created startSlide function for sliding images every 4 seconds.
  • Created next and prev functions for sliding to the previous or the next image. According to the last currentImg function it detects which image must show at that time based on the index.

It’s time to add the markup/HTML part to the component:

    <transition-group name="fade" tag="div">
      <div v-for="i in [currentIndex]" :key="i">
        <img :src="currentImg" />
    <a class="prev" @click="prev" href="#">&#10094; Previous</a>
    <a class="next" @click="next" href="#">&#10095; Next</a>

Here we take advantage of the built-in transtion-group component that comes with Vue.js, then iterate images and add the functions we created earlier.

To make this component look great we need to add some CSS styling to it:

.fade-leave-active {
  transition: all 0.9s ease;
  overflow: hidden;
  visibility: visible;
  position: absolute;
  opacity: 1;

.fade-leave-to {
  visibility: hidden;
  opacity: 0;

img {

.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 40%;
  width: auto;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.7s ease;
  border-radius: 0 4px 4px 0;
  text-decoration: none;
  user-select: none;

.next {
  right: 0;

.prev {
  left: 0;

.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.9);

As it was mentioned earlier we used the built-in transition-group component from Vue.js which has ready-made class names like fade-enter-active, fade-leave-active, among others.

Making changes in App.vue

Let’s not forget to check App.vue and import our Slider component:

  <div id="app">
    <Slider />

import Slider from "./components/Slider.vue";
export default {
  name: "app",
  components: {

#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
body {
  margin: 0px;

That’s it! Our image slider is ready to be used! You can find the full source code on Github here.

vue-js javascript image web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

Hire Web Developer

Looking for an attractive & user-friendly web developer?, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.