Building a card carousel component in Vue JS

Struggling with creating a unique card slider using Vue JS? After exploring npm packages like Vue Carousel 3D and Vue Slick, I haven't found the ideal slider for my project. My specific card slider setup is showcased below:

https://i.sstatic.net/GiNcn.png

In this design, there are three images displayed - one in front and two behind. The functionality I aim to achieve involves pushing the front image backward upon button press, while bringing the back image forward.

https://i.sstatic.net/kHjKO.png

I've scoured the web extensively, even delving into pure JavaScript options, yet have failed to find a suitable example. Any assistance you can provide would be greatly appreciated.

index.html

<div class="first_block">
    <h2>FEATURED SHOWS</h2>
    <div class="girls_gard_container">
      <img class="card_1" src="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80" alt="Girl">
      <img class="card_2" src="https://images.unsplash.com/photo-1527455505333-9d3ac7adf523?ixid=MXwxMjA3fDB8MHxzZWFyY2h8OXx8Zml2ZXxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80" alt="Girl">
      <img class="card_3" src="https://images.unsplash.com/photo-1597976618063-810eb50c84fb?ixid=MXwxMjA3fDB8MHxzZWFyY2h8NHx8dGFtfGVufDB8fDB8&ixlib=rb-1.2.1&w=1000&q=80" alt="Girl">
    </div>
</div>

style.css

.first_block {
    padding: 0px 23px 0px 23px;
    margin: 5px;
  }
  
  .circle-wrap {
    margin: 0px 5px 0px 5px;
  }
  
  .third_block div h2 {
    font-size: 20px;
    font-family: Montserrat-Medium;
  }
  
  .first_block {
    width: 30%;
  }
  
  .first_block h2, .second_block h2 {
    font-family: Montserrat-Medium;
    margin-bottom: 0.3rem;
  }
  
  .first_block h2 {
    text-align: center;
    font-size: 20px;
  }

  .girls_gard_container {
    position: relative;
    bottom: 15px;
  }

  .card_1 {
    position: absolute;
    max-width: 100%;
    top: 70px;
    width: 100px;
    height: 238px;
  }
  
  .card_2 {
    position: absolute;
    max-width: 100%;
    top: 44px;
    left: 15px;
    width: 126.24px;
    height: 287px;
  }
  
  .card_3 {
    position: absolute;
    max-width: 100%;
    top: 20px;
    left: 25px;
    width: 240px;
    height: 331px;
  }

Answer №1

Utilizing the existing styling, you have the option to manually cycle through it yourself. While the following example may not be the most aesthetically pleasing and there is likely a cleaner alternative to achieve this, here's an illustration:

<template>
  <div>
    <div class="first_block">
      <button v-on:click="moveToNextCard()">Next</button>

      <h2>FEATURED SHOWS</h2>
      <div class="girls_gard_container">
        <img
          class="card_1"
          :src="cards[index % cards.length].img_url"
          alt="Girl"
        />
        <img
          class="card_2"
          :src="cards[(index + 1) % cards.length].img_url"
          alt="Girl"
        />
        <img
          class="card_3"
          :src="cards[(index + 2) % cards.length].img_url"
          alt="Girl"
        />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    msg: String,
  },
  data() {
    return {
      index: 0,
      cards: [
        {
          id: 1,
          img_url:
            "https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80",
        },
        {
          id: 2,
          img_url:
            "https://images.unsplash.com/photo-1527455505333-9d3ac7adf523?ixid=MXwxMjA3fDB8MHxzZWFyY2h8OXx8Zml2ZXxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80",
        },
        {
          id: 3,
          img_url:
            "https://images.unsplash.com/photo-1597976618063-810eb50c84fb?ixid=MXwxMjA3fDB8MHxzZWFyY2h8NHx8dGFtfGVufDB8fDB8&ixlib=rb-1.2.1&w=1000&q=80",
        },
      ],
    };
  },
  methods: {
    moveToNextCard() {
      this.index = (this.index + 1) % this.cards.length;
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.first_block {
  padding: 0px 23px 0px 23px;
  margin: 5px;
}

.circle-wrap {
  margin: 0px 5px 0px 5px;
}

.third_block div h2 {
  font-size: 20px;
  font-family: Montserrat-Medium;
}

.first_block {
  width: 30%;
}

.first_block h2,
.second_block h2 {
  font-family: Montserrat-Medium;
  margin-bottom: 0.3rem;
}

.first_block h2 {
  text-align: center;
  font-size: 20px;
}

.girls_gard_container {
  position: relative;
  bottom: 15px;
}

.card_1 {
  position: absolute;
  max-width: 100%;
  top: 70px;
  width: 100px;
  height: 238px;
}

.card_2 {
  position: absolute;
  max-width: 100%;
  top: 44px;
  left: 15px;
  width: 126.24px;
  height: 287px;
}

.card_3 {
  position: absolute;
  max-width: 100%;
  top: 20px;
  left: 25px;
  width: 240px;
  height: 331px;
}
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JWT authentication for restricted routes

I'm currently developing an application that requires users to log in and allows them to join private groups. I have successfully implemented the login part using JWT, but I'm struggling with how to prevent users from joining private groups until ...

Using PHP and JQuery to disable a button after the letter "U" is typed

I am looking for a way to disable the button when the term "U" (defined as Unable) appears. How can I achieve this? Below is the button in question: <input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input& ...

The D3.extent() function is raising a TypeError because it is unable to iterate over the

I have been struggling with this issue for the past few hours, and I could really use some help. I'm not very familiar with using D3 in conjunction with React. Essentially, I have an anomaly detection algorithm running on the backend Flask App that r ...

Adding Font Awesome to your Vue and Nuxt.js project: A step-by-step guide

So, I attempted to include the necessary CSS by adding this line: css: ['@/assets/front_end/fontawesome-free-5.15.3-web/css/fontawesome.css'], However, instead of displaying correctly, it appeared as an image like in the following link: ...

How do you utilize the beforeMount lifecycle method in Vue.js?

I am currently working on creating a weather application using Vue.js. I have successfully obtained the latitude and longitude when the window loads, and I have set these values to variables lat and long. Despite this, I am encountering issues when tryin ...

Dynamic options can now be accessed and modified using newly computed getters and setters

When using Vuex with Vue components, handling static fields that are editable is easily done through computed properties: computed: { text: { get() { return ... }, set(value) { this.$store.commit... }, }, }, <input type ...

Count the number of distinct values in an array of objects

Could you assist me with a JavaScript problem I'm experiencing? I have an array and need to extract the count key from the response. Here is a sample response: var events = [ ... I would like the array response to look like this: var events = [ ... ...

Elegant Decline of Javascript Functionality - Imported Web Assets

Looking for assistance from experienced JS coders. I'm currently working on a Wordpress website that utilizes jQuery AJAX methods to reload either the entire content area or just the main content section based on different navigation clicks. My aim i ...

Guide on how to gather values into a variable from the DOM using jQuery

Trying to make this function: The current issue I'm facing is that when changing a digit (by clicking on a hexagon), I want to save the selected number as the value of a hidden input field next to the digit in the DOM. Any ideas on how to achieve th ...

Intrigued by the connection between background and calc()?

Hello everyone! I'm currently involved in a project and have a quick question regarding the calc function. As we all know, it functions perfectly on all browsers... ========== great! ============= background-color:#dfdfdf; background-image:url(..) ...

Ways to simulate the route object in Vue 3 composition functions?

I'm looking to simulate the route object in order to avoid test failures such as TypeError: Cannot read properties of undefined when trying to access route.x. Here's what I attempted: const route = { fullPath: '/', path: '/' ...

Is there a problem with addEventListener returning false?

What does keeping the third parameter as false in the line below signify? var el = document.getElementById("outside"); el.addEventListener("click", modifyText, false); ...

What is the method to effectively conduct a testing procedure for JavaScript files that have been exported using

I have a JavaScript file called "sum.js" which contains a simple function: // sum.js function sum(a, b) { return a + b; } export default { sum }; Now I want to write a test for this file using Jest. Here is my "sum.test.js" file in the same folder: // ...

Tips for modifying the hue of the hint attribute within vue.js?

`<v-text-field id="loginPasswordId" ref="password" v-model="password" class="login-input" dense :disabled="loading" :hint="hello world" :loading="loading" maxlength= ...

What is the reason for the failure of the jQuery code to disable the submit button in the following snippet?

I am working on a feature to disable the submit button in a form when the username, email, password fields are empty. When all of them are filled, I want to enable the submit button. However, the current code is not disabling the submit button as expected. ...

Curious about the power of jQuery methods?

I've been coming across codes similar to this: $(document.documentElement).keyup( function(event) { var slides = $('#slides .pagination li'), current = slides.filter('.current'); switch( event.keyCode ) { ...

The React material-table only updates and rerenders the table when the data is updated twice

Currently, I am utilizing a tool called material-table (check it out here: https://material-table.com/#/) which has been developed using React. The issue I am facing is that the data passed as a prop to material-table doesn't seem to update correctly ...

Choose a numeric value and then adjust it to display with exactly two decimal places

My goal is to create a code that achieves the following tasks: Add an ID to every nth child Round the number in each nth child to 2 decimal places Prefix the numbers with a pound sign (£) Loop through until all the nth children in a table are processed ...

Angular HttpClient does not support cross-domain POST requests, unlike jQuery which does

I am transitioning to Angular 13 and I want to switch from using jQuery.ajax to HttpClient. The jquery code below is currently functional: function asyncAjax(url: any){ return new Promise(function(resolve, reject) { $.ajax({ type: ...

Dealing with undefined or null values when using ReactJS with Formik

Issue Resolved: It seems that Formik requires InitialValues to be passed even if they are not necessary. I'm currently working on a formik form in React, but every time I click the submit button, I encounter an error message stating "TypeError: Canno ...