Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Vue.js Basics Building a Flashcard App Flip Transition!

Sarika Rani
Sarika Rani
11,214 Points

why its deleting card aftre adding transition while i click on card ?

Hi,

i added transition. it suppose to flip but it is deleting card when i click on li item. I dont knw what is the problem ..

<div id="flashcard-app" class="container">
    <h1>Flash Card</h1>
    <div class="flashcard-form">
        <label for="front">Front
            <input v-model="newFront" type="text" id="front">
        </label>
        <label for="back">Back
            <input v-on:keypress.enter="addNew" v-model="newBack" type="text" id="back">
        </label>
        <button v-on:click="addNew">Add a new Card</button>
        <span class="error">Oops! Flash card need front and back entry</span>
    </div>
    <ul class="flashcard-list">
        <li v-on:click="toggleCard(card)" v-for="(card, index) in cards">
            <transition>
                <p v-show="!card.flipped" key="front" class="card">{{card.front}}
                    <span v-on:click="cards.splice(index, 1)" class="delete-card">X</span>
                </p>
                <p v-show="card.flipped" key="back" class="card"> {{card.back}}
                    <span v-on:click="cards.splice(index, 1)" class="delete-card">X</span>
                </p>
            </transition>
        </li>
    </ul>
</div> ;

.flip-enter-active { transition: all 0.4s ease; }

.flip-enter { transform: rotateY(180deg); opacity: 0; }

.flip-leave { display: none; }

.flashcard-form { position: relative; }

label { font-weight: 400; color: #333; margin-right: 10px; }

new Vue({ el: '#flashcard-app', data: { cards: cards, newFront: '', newBack: '', }, methods: { toggleCard : function(card) { card.flipped = !card.flipped; }, addNew: function() { this.cards.push({ front: this.newFront, back: this.newBack, flipped: false }) } }, });

3 Answers

Andrew Biviano
Andrew Biviano
6,748 Points

It's bc you're using v-show rather than v-if...they function differently see the below comment from the instructor

This has to do with the way the <transition> component works. If you look in your console, you should see the following error:

vue.js:584 [Vue warn]: <transition> can only be used on a single element. Use <transition-group> for lists.

When you use v-show, you're toggling the CSS display property on and off, so both paragraph elements (the front and back of the card) technically always exist in the DOM at the same time, you're just showing or hiding them (temporarily delete the <transition> element-- you'll see that both paragraphs are always there, but on click display: none is being added and removed from both elements).

The existence of two elements at once causes the <transition> component to throw a fit because it can only deal with one element at a time. With v-if and v-else, the front and backs of the cards are actually being added and removed from the DOM on click. This ensures that only one paragraph element at a time exists in the DOM, and the <transition> component is happy :D

The <transition-group> is designed to handle more than one element. Change <transition> to <transition-group> and v-show should work for you.

Libor Gess
Libor Gess
6,563 Points

Hello Sarika, have you find the solution?

Elisa Burghard
Elisa Burghard
9,276 Points

Hey Sarika, the first thing I noticed: the <transition> tag does not include name="flip" Add it and this might fix your problem: <transition name="flip">

Happy Coding!