JAVASCRIPT

Applying Basic Fade Transitions in Vue 3

Learn to use Vue 3's built-in `<Transition>` component to add smooth fade-in and fade-out animations when elements are conditionally rendered.

<!-- App.vue -->
<template>
  <div>
    <button @click="show = !show">Toggle Element</button>

    <Transition name="fade">
      <p v-if="show">Hello, Vue 3 Transitions!</p>
    </Transition>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const show = ref(true);
</script>

<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
How it works: This snippet demonstrates how to implement a basic fade transition for elements that are conditionally rendered or toggled using `v-if` or `v-show` in Vue 3. The `<Transition>` component wraps the element, and CSS classes prefixed with the `name` prop (e.g., `fade-enter-from`, `fade-enter-active`, `fade-leave-to`, `fade-leave-active`) are automatically applied by Vue during the enter and leave transitions. By defining CSS `transition` properties and `opacity` changes for these classes, you can create smooth visual effects for element appearances and disappearances.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs