The Be Sure Blog

Code Snippets | Problem Solving | Tips & Tricks

The Be Sure Blog banner

Building a custom image slider in Vue.js

posted on 1.2.2023 by Below Surface in "Vue.js"

Note: We are using Vue 3 here and I'm new to it myself.

Firstly, we need an array of images, alt texts and optional: a link to click on and text to display alongside the image:

const sliderArray = [
        {   image: "/images/references/web/besure1.jpg", 
            alt: "Alt text",
            link: "https://besure.blog",
            text: "Be Sure Blog Image"
        },
        {   image: "/images/references/web/besure2.jpg",
            alt: "Alt text",
            link: "https://besure.blog",
            text: "Be Sure Blog Image"
        },
        {   image: "/images/references/web/besure3.jpg",
            alt: "Alt text",
            link: "https://besure.blog",
            text: "Be Sure Blog Image"
        }
    ];

Important: The images must exist in the folder "public/images/references/web"!

Next, we will use the ref method, which seems like it was introduced in Vue 3. I had an issue with Vue not re-rendering on state change, and my research led me to this solution (resource link is at the end of the blog post):

import { ref } from 'vue';
const reactiveCount = ref(0);

Then:

export default {
    data() {
        return {
            sliderArray,
            currentIndex: 0
        }
    },
    methods: {
        next: function() {
            reactiveCount.value += 1;
        },
        prev: function() {
            reactiveCount.value -= 1;
        }
    },
    computed: {
        currentSlide: function() {
            return sliderArray[Math.abs(reactiveCount.value) % sliderArray.length];
        }
    }
}

Now we have all the <script> functionalities in place and can work on the <template> part:

<div class="image-slider-container">
    <div class="left-arrow" @click="prev">&#10094;</div>
    <div class="right-arrow" @click="next">&#10095;</div>
    <div class="image-slider">
        <transition-group name="fade" tag="div">
            <div v-for="i in [currentIndex]" :key="i">
                <img :src="currentSlide.image" :alt="currentSlide.alt" />
                <a :href="currentSlide.link" target="_blank" rel="noreferrer">
                    <p>{{ currentSlide.text }}</p>
                </a>
            </div>
        </transition-group>
    </div>  
</div>

Finished! Check out the two resources below this post for more information!

Tags:

vue.js
image slider

Sources:

https://www.digitalocean.com/community/tutorials/vuejs-create-image-sliderhttps://michaelnthiessen.com/debugging-guide-why-your-component-isnt-updating/

More posts of this category

Getting started with global state management in Vue.js

How to use the Vue.js global store called Vuex

Vue.js

Add smooth scrolling to a Vue 3 website

Adding smooth scrolling usually makes up for a much better UX, and it is easy to implement

Vue.js

Install and use the router in Vue 3

Learn how to implement page routing in Vue 3

Vue.js

How to send emails from a Vue 3 client app

Learn how to use Emailjs to send emails from your client app

Vue.js