Bird
Raised Fist0
Vueframework~20 mins

Enter and leave transitions in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue Transition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when the <Transition> component wraps a conditionally rendered element?

Consider this Vue 3 code snippet using the <Transition> component:

<template>
  <Transition name="fade">
    <div v-if="show">Hello</div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
</script>

What is the visible behavior when show changes from true to false?

Vue
<template>
  <Transition name="fade">
    <div v-if="show">Hello</div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
</script>
AThe <div> disappears immediately without any animation.
BThe <div> remains visible even if <code>show</code> is false.
CThe <div> fades in when shown but disappears immediately when hidden.
DThe <div> fades out smoothly before being removed from the DOM.
Attempts:
2 left
💡 Hint

Think about what the <Transition> component does with elements entering and leaving the DOM.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a CSS fade transition for Vue's <Transition> component?

You want to create a fade effect for entering and leaving elements using Vue's <Transition> with name="fade". Which CSS snippet is correct?

A.fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter-from, .fade-leave-to { opacity: 0; } .fade-enter-to, .fade-leave-from { opacity: 1; }
B.fade-enter, .fade-leave { transition: opacity 0.5s; } .fade-enter-active, .fade-leave-active { opacity: 0; }
C.fade-enter-active { opacity: 1; } .fade-leave-active { opacity: 0; transition: opacity 0.5s; }
D.fade-enter-from, .fade-leave-from { transition: opacity 0.5s; } .fade-enter-to, .fade-leave-to { opacity: 1; }
Attempts:
2 left
💡 Hint

Remember Vue uses -enter-active and -leave-active classes for transitions and -from/-to for start/end states.

❓ state_output
advanced
2:00remaining
What is the final DOM state after toggling a <Transition> wrapped element off?

Given this Vue 3 component:

<template>
  <button @click="show = !show">Toggle</button>
  <Transition name="slide">
    <p v-if="show">Slide me</p>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
</script>

After clicking the button to set show to false, what happens to the <p> element in the DOM?

Vue
<template>
  <button @click="show = !show">Toggle</button>
  <Transition name="slide">
    <p v-if="show">Slide me</p>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
</script>
AThe <p> element animates sliding out and then is removed from the DOM.
BThe <p> element is immediately removed from the DOM with no animation.
CThe <p> element remains in the DOM but is hidden with CSS.
DThe <p> element stays visible and does not respond to the toggle.
Attempts:
2 left
💡 Hint

Think about how Vue handles leave transitions with <Transition>.

🔧 Debug
advanced
2:00remaining
Why does this Vue transition not animate on leave?

Look at this Vue 3 code:

<template>
  <Transition name="fade">
    <div v-if="visible">Content</div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'
const visible = ref(true)
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
.fade-enter-to {
  opacity: 1;
}
</style>

When visible changes from true to false, the element disappears immediately without fading out. Why?

Vue
<template>
  <Transition name="fade">
    <div v-if="visible">Content</div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'
const visible = ref(true)
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
.fade-enter-to {
  opacity: 1;
}
</style>
AThe transition duration is too short to notice the animation.
BThe <code>v-if</code> directive is not reactive, so the transition does not trigger.
CThe CSS is missing the .fade-leave-from class, so leave animation cannot start properly.
DThe transition name 'fade' is not correctly applied to the element.
Attempts:
2 left
💡 Hint

Check if all required CSS classes for enter and leave states are defined.

🧠 Conceptual
expert
3:00remaining
How does Vue's <Transition> component manage multiple elements leaving simultaneously?

When multiple elements inside a <TransitionGroup> leave at the same time, how does Vue ensure smooth animations?

AVue removes all elements immediately and applies a global fade-out animation to the container.
BVue delays removing each element until its individual leave animation finishes, tracking each separately.
CVue queues the leave animations to run one after another, never overlapping.
DVue clones the elements and animates the clones while removing the originals instantly.
Attempts:
2 left
💡 Hint

Think about how Vue handles asynchronous animations for multiple elements.

Practice

(1/5)
1. What is the main purpose of the <transition> component in Vue?
easy
A. To handle user input events
B. To add animations when elements enter or leave the DOM
C. To create reactive data properties
D. To fetch data from an API

Solution

  1. Step 1: Understand the role of <transition>

    The <transition> component is designed to wrap elements and apply animations when they appear or disappear.
  2. Step 2: Compare with other options

    Fetching data, creating reactive properties, and handling events are unrelated to <transition> functionality.
  3. Final Answer:

    To add animations when elements enter or leave the DOM -> Option B
  4. Quick Check:

    Enter and leave animations [OK]
Hint: Think: transition means animation on show/hide [OK]
Common Mistakes:
  • Confusing transition with data fetching
  • Thinking transition handles events
  • Mixing transition with reactive data setup
2. Which of the following is the correct way to apply a CSS transition named "fade" to an element in Vue?
easy
A.
Content
B.
Content
C.
Content
D. Content

Solution

  1. Step 1: Check correct usage of <transition> with name

    The <transition> component requires a name attribute to link CSS classes, and the element to animate must be inside it with v-if or v-show.
  2. Step 2: Validate each option

    <transition name="fade"><div v-if="show">Content</div></transition> correctly wraps the element inside <transition name="fade"> with v-if. Using transition as an attribute on div is invalid, placing v-if on <transition> does not work properly, and using class on div without name on <transition> misses the name attribute.
  3. Final Answer:

    <transition name="fade"><div v-if="show">Content</div></transition> -> Option A
  4. Quick Check:

    Use <transition name="fade"> wrapper [OK]
Hint: Always put v-if inside <transition name="..."> [OK]
Common Mistakes:
  • Putting v-if on <transition> instead of child
  • Missing the name attribute on <transition>
  • Using transition attribute on div instead of component
3. Given this Vue template and CSS, what happens when show changes from false to true?
<template>
  <transition name="slide">
    <div v-if="show">Hello</div>
  </transition>
</template>

<style>
.slide-enter-active { transition: all 0.5s ease; }
.slide-enter-from { transform: translateX(-100%); opacity: 0; }
.slide-enter-to { transform: translateX(0); opacity: 1; }
</style>
medium
A. The div fades out when shown
B. The div instantly appears without animation
C. The div slides out to the right when shown
D. The div slides in from the left with fading effect

Solution

  1. Step 1: Understand the transition classes

    The classes slide-enter-from and slide-enter-to define the start and end states for entering. The div starts translated left and transparent, then moves to normal position and full opacity.
  2. Step 2: Analyze the effect when show changes true

    When show becomes true, the div enters with the slide-enter transition, sliding in from left and fading in.
  3. Final Answer:

    The div slides in from the left with fading effect -> Option D
  4. Quick Check:

    slide-enter-from to slide-enter-to = slide in + fade [OK]
Hint: Enter transitions use -enter-from to -enter-to classes [OK]
Common Mistakes:
  • Confusing enter and leave animations
  • Ignoring transform and opacity in CSS
  • Assuming no animation on v-if change
4. Identify the error in this Vue transition code:
<transition name="fade">
  <div v-show="visible">Content</div>
</transition>

<style>
.fade-leave-active { transition: opacity 0.3s ease; }
.fade-leave-from { opacity: 1; }
.fade-leave-to { opacity: 0; }
</style>
medium
A. The transition name should be "fade-leave" instead of "fade"
B. Using v-show instead of v-if prevents leave transition from running
C. Missing fade-enter classes causes no enter animation
D. The CSS transition duration is too short to see

Solution

  1. Step 1: Examine the provided CSS classes

    Only leave transition classes (.fade-leave-active, .fade-leave-from, .fade-leave-to) are defined. No enter classes (.fade-enter-active, .fade-enter-from, .fade-enter-to) are present.
  2. Step 2: Determine the impact

    Missing enter classes (.fade-enter-*) means no animation occurs when the element enters (visible changes from false to true). The leave transition works fine with v-show.
  3. Final Answer:

    Missing fade-enter classes causes no enter animation -> Option C
  4. Quick Check:

    Missing enter classes = no enter animation [OK]
Hint: Define enter-active/from/to for enter animations [OK]
Common Mistakes:
  • Thinking v-show prevents leave animations (it doesn't)
  • Proposing to change transition name
  • Focusing on duration instead of missing classes
5. You want to create a fade transition that only triggers when a list item is removed, not when added. Which approach correctly achieves this in Vue?
hard
A. Use <transition> with only leave-active and leave-from/to CSS classes, no enter classes
B. Use <transition> with enter and leave classes but set v-show instead of v-if
C. Use <transition> with enter classes only and no leave classes
D. Use <transition> with both enter and leave classes and toggle v-if normally

Solution

  1. Step 1: Understand transition class roles

    Enter classes control animation when element appears; leave classes control animation when element disappears.
  2. Step 2: To animate only removal, define leave classes only

    By omitting enter classes, no animation runs on enter, but leave animations run on removal.
  3. Final Answer:

    Use <transition> with only leave-active and leave-from/to CSS classes, no enter classes -> Option A
  4. Quick Check:

    Only leave classes = animate removal only [OK]
Hint: Omit enter classes to animate only leave transitions [OK]
Common Mistakes:
  • Using v-show prevents leave animations
  • Defining enter classes triggers animation on add
  • Confusing enter and leave class roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard