Home > Web Front-end > Vue.js > body text

How to use CSS transitions to achieve animated transition effects in Vue

PHPz
Release: 2023-06-11 08:00:12
Original
1378 people have browsed it

Vue is a modern JavaScript framework that provides an easy way to build interactive interfaces and single-page applications. In Vue, we can easily use CSS animations to achieve smooth transition effects.

In Vue, we use the component to implement CSS transition animation. This component can wrap any element that needs to transition to another state, such as adding, removing, or updating elements. The following is a simple example that shows how to use the component to implement the fade-in and fade-out effect of an element:

<template>
  <div>
    <button @click="show = !show">Toggle</button>
    <transition name="fade">
      <div v-if="show">Hello World</div>
    </transition>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>
Copy after login

In this example, the component wraps an element that needs to be transitioned. This The element will only be displayed if show is true. By clicking the Toggle button, we can switch the value of show to demonstrate the fade effect.

The CSS animation style is defined in the class name specified by the name attribute of the component. In this example, we define a transition component called fade and define CSS transition animations for its enter and leave states.

  • The .fade-enter-active and .fade-leave-active classes are used to define the duration and transition effects of animations.
  • The .fade-enter and .fade-leave-to classes are used to define the styles at the beginning and end of the transition.

Vue will automatically add and remove these class names to trigger CSS transition animations when elements enter and leave.

In addition to the fade in and fade out effect, we can also use CSS transition animation to achieve a variety of complex transition effects, such as movement, rotation, scaling, etc. The following is an example of implementing an element rotation effect:

<template>
  <div>
    <button @click="show = !show">Toggle</button>
    <transition name="rotate">
      <div v-if="show" class="box"></div>
    </transition>
  </div>
</template>

<style>
.rotate-enter-active, .rotate-leave-active {
  transition: transform 1s;
}
.rotate-enter, .rotate-leave-to {
  transform: rotate(0deg);
}
.rotate-leave, .rotate-enter-to {
  transform: rotate(180deg);
}
.box {
  width: 100px;
  height: 100px;
  background-color: red;
}
</style>
Copy after login

In this example, we define a transition component named rotate and define CSS transition animations for its enter, leave, and transition states. When an element enters, it rotates from 0 degrees to 180 degrees, and when an element leaves, it rotates from 180 degrees to 0 degrees.

To summarize, CSS transition animation can be easily used in Vue to achieve various transition effects. Through the component, we can easily define the entry, exit, and transition states of the animation, and use CSS styles to customize the animation effect. This gives our application a better user experience and sense of interactivity.

The above is the detailed content of How to use CSS transitions to achieve animated transition effects in Vue. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!