5

I have a sort of simple question. Im building a carousel which rotates vertically using vue. When the user clicks the forward button, I want vue to transition the elements in/out using my 'forward' transition (moving them down). When the user clicks the backward button, I want vue to transition the elements in/out using my 'backward' transition (moving them up).

Right now, I just have one transition that performs my forward transition, whether the user is navigating forward or backward.

Is there any way to accomplish this?

Heres my component:

<template>
<div class="carousel__container">
    <div class="carousel__visible">
        <div class="carousel__slides">

            <transition-group name="carouselNext">
                <div v-for="(quote, index) in quotes" class="carousel__slide" key="index" v-show="index === currentSlide">
                    <blockquote>{{ quote.quote }}</blockquote>
                    <cite>{{ quote.cite }}</cite>                   
                </div>
            </transition-group>

        </div>


    </div>

    <button class="btn btn--alt" @click="prev">Back</button>

    <button class="btn btn--primary" @click="next">Next</button>
</div>

Thanks!

1 Answer 1

10

Easiest way is to change the transition name based on direction <transition-group name="carouselNext"> can become <transition-group :name="direction">

add new direction data var and include toggling based on methods

    prev(){
      this.direction = "carouselPrev"
      this.currentSlide = (this.currentSlide + this.quotes.length - 1)%this.quotes.length
    },
    next(){
      this.direction = "carouselNext"
      this.currentSlide = (this.currentSlide + 1)%this.quotes.length
    }

and finally add the alternate carouselPrev css transition classes

here's a working example https://codepen.io/scorch/pen/NwwpxM

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.