Display only three slides using Vue.js

I'm working with a Carousel component:

<template>
  <div class="flex items-center justify-between">
    <div class="w-1/5">
      <div class="pb-6">
        <h2 class="pb-6">
          My works
        </h2>
        <p>
          Aliqua id fugiat nostrud irure ex duis ea quis id quis ad et.
        </p>
      </div>
      <div class="flex items-center slide-pagination">
        <span class="cursor-pointer slide-prev" @click="prev">&lt;<span class="text-grey-primary pl-1">/</span></span>
        <div class="px-3 flex items-center carousel-dots">
          <span class="mr-3 active" />

          <span class="mr-3" />
          <span class="w-1 h-1" />
        </div>
        <span class="cursor-pointer slide-next" @click="next"><span class="text-grey-primary pr-1">/</span>&gt;</span>
      </div>
    </div>
    <div class="carousel-clients-works flex w-3/4 justify-between overflow-hidden">
      <CarouselSlide v-for="(block,index) in blocks" :key="index" :index="index" :slide-current="slideCurrent">
        <div class="carousel-block mb-6" style="margin-left: auto; margin-right: auto;">
          <img :src="block.src" :alt="block.title">
        </div>

        <div class="text-center w-80">
          <h3 class="font-bold" v-text="block.title" />
          <ul v-for="(list, ind) in block.lists" :key="ind" class="disc-green">
            <li v-text="list.title" />
          </ul>
        </div>
      </CarouselSlide>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    blocks: {
      type: Array,
      default: null
    }
  },
  data: () => ({
    slideCurrent: 0
  }),
  computed: {
    slidesLen () {
      return this.blocks.length
    }
  },
  methods: {
    prev () {
      if (this.slideCurrent <= 0) {
        this.slideCurrent = this.slidesLen - 1
      } else {
        this.slideCurrent--
      }
    },
    next () {
      if (this.slideCurrent >= this.slidesLen - 1) {
        this.slideCurrent = 0
      } else {
        this.slideCurrent++
      }
    }
  }
}
</script>

Additionally, there is a child component called CarouselSlide:

<template>
  <div v-show="slideCurrent === index">
    <slot />
  </div>
</template>

<script>
export default {
  props: {
    slideCurrent: {
      type: Number,
      default: 0
    },
    index: {
      type: Number,
      default: 0
    }
  }
}
</script>

I need to display 3 slides on my carousel. The issue I'm facing is that the slideCurrent value is only 1 number, while in the child component CarouselSlide I'm checking the current Slide with the loop index. How can I resolve this issue? Any help or suggestions would be greatly appreciated. I have tried using a filter, but it doesn't meet my requirements.

Answer №1

To display only the 3 desired slides, you can create a computed property:

computed: {
  selectedSlides() {
    const numSelected = 3;
    const selectedSlides = [];

    for(let i = 0; i < numSelected; i++) {
      const index = (this.slideCurrent + i) % this.slides.length;
      selectedSlides.push(this.slides[index]);
    }
    return selectedSlides;
  }
}

In your template, iterate over selectedSlides instead:

<SlideComponent v-for="(slide, index) in selectedSlides" ... >

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

When attempting to compress JavaScript with uglify-js, an unexpected token error occurs with the symbol ($)

When attempting to compress Bootstrap 4 js file using uglify-js, I encountered an error. The error message reads as follows: "Parse error at src\bootstrap\alert.js:1,7 import $ from 'jquery' ERROR: Unexpected token: name ($)". Now I am ...

What steps should I take to move the content to the bottom of the page once the promise is fulfilled within the function?

I have a showBoxConfirm function that confirms user actions. After clicking the button, it triggers the clickMethod function. The result variable will store the confirmation response, and if it returns false, the function will terminate. However, the sho ...

Exporting the state of a Redux reducer as an array in a TypeScript file

Having an issue where my reducer is undefined and I can't seem to figure out why. It's working fine when it's not an array, but when it is an array it stops working. This is how my code looks like in groupSlice.ts: export interface GroupSta ...

Using jQuery to verify the presence of an element, especially one that may have been dynamically inserted via AJAX

I have a good understanding of how to verify elements that are present when the document is loaded: jQuery.fn.exists = function () { return jQuery(this).length > 0; } However, this approach does not detect elements that are dynamically added via A ...

Determine the closest parent using jQuery

When using jQuery, the closest function can be called to locate the nearest parent element. For instance, if there is an a within a li within a ul within a td within a table, determining whether the ul parent is closer than the table parent may not always ...

Help needed with using Regex to restrict the number of alphabetical characters allowed

Struggling with configuring RegEx's. Seeking guidance to create a RegEx with the following criteria: Only allow numbers (0-9) Allow a period (.), negative sign (-), plus sign (+), dollar sign ($), and comma (,) Do not permit any alphabetic characte ...

Backend JS error found in Joomla 3.0 Component

Currently, I am working on developing a custom Joomla 3.0 component. To begin, I started by downloading the com_hello component sample from the official Joomla documentation. However, I've encountered an issue when trying to check the checkbox in the ...

What is the best way to retrieve the value from local storage?

const value = document.getElementById("demo").getAttribute('value'); if(typeof(Storage)!=="undefined") { alert(value); localStorage.setItem("GetData", value); alert(localStorage.getItem("GetData")); } function loading() { alert("coming" ...

Trouble with JavaScript event listener/button functionality

Seeking a simple fix here. While I have experience in Java, I am slowly but surely delving into Javascript. Currently, I am working on creating a fun website. The specific task at hand involves utilizing the parse api. My approach (as there seemed to be ...

Is it possible to securely embed videos on external websites while also utilizing tokens to safeguard the content?

Protecting our video content on our website is a top priority, which is why we have implemented a system where a token is grabbed through AJAX and verified through PHP before allowing the download of any files. As I delve into providing an embed feature s ...

Choose just one button from the variety of buttons available

I am facing an issue with a component that contains multiple cards, each with its own button. Below is the code for this component: import React, { useState, useEffect } from "react"; import "./App.css"; import { Card, Container, Row, C ...

Switching Divs Based on Radio Button Selection in VueJS

How can I display different components based on radio button selection? <input type="radio" name="book" value="One" checked="checked"> <input type="radio" name="book" value="Round"> <div> // initial display when "One" is selecte ...

What is the best way to display toastr messages in an Angular application?

Can you guide me on how to include toastr in an angular app? Currently, I am enrolled in the Angular Fundamentals course and trying to use toastr.success within my export class: handleThumbnailClick(eventName){ toastr.success(eventName) } But, I kee ...

Encountered an issue with fs.open where a non-literal argument was used at index 0 while utilizing a url

Attempting to achieve something similar in TypeScript: window.open(`https://somelink/certificate/${regNumber}?registrationNumber=${type}`); where the values of regNumber and type are constantly changing. ESLint is reporting an issue: Received fs.open with ...

Fixed columns with horizontal scrolling to prevent Datatables columns from overlapping

I'm facing an issue with my data table created using datatables. I need to fix the first two columns, but when I do so, the other two columns overlap them while scrolling horizontally. If I remove the fixed columns, the scrolling works correctly witho ...

Tips for assigning a cookie as the id of a div?

I've been tasked with making the selected menu stand out when the page is refreshed. I'm thinking of using a cookie to achieve this. Here's the HTML code: <div class="menuBar"> <div class="menuHeader ui-corner-top"> ...

What is the correct way to invoke a function contained within an object that is stored in an array?

I've encountered a problem with my program. I'm attempting to invoke a function that is part of an object stored in an array, but I'm having difficulty calling the function correctly. //Initialize Array for Storing Projects let allProjects ...

The Vue component is successfully rendering on localhost, but is not displaying on the server when using Laravel 5.4 with Passport

I attempted to configure an Oauth2.0 server using Passport within Laravel 5.4 by following the steps outlined at . I successfully set it up on my local host, but encountered issues when deploying the code to my production server. Upon accessing the applica ...

Disable the scroll animation feature for certain IDs

I implemented JavaScript code to animate scrolling for each block with a specific ID. However, when I added Bootstrap's tabs, the animation interfered with the functionality of the tabs. Is there a way to disable the scroll animation specifically for ...

Encountering an issue when utilizing a personalized directive with AngularJS

I'm encountering an issue with the "auto-complete" directive that I'm using from jsfiddle. The error message I'm receiving is iElement.autocomplete is not a function. Can someone help me troubleshoot and fix this error? directive.js starte ...