Home Web Front-end Vue.js Learn custom Transition animation in Vue 3 to create cool page effects

Learn custom Transition animation in Vue 3 to create cool page effects

Sep 10, 2023 am 11:06 AM
vue Animation effects Custom transition

学习Vue 3中的自定义Transition动画,打造炫酷的页面效果

In today’s Internet era, the coolness and personalization of page effects have become one of the important factors that attract users’ attention. In the field of front-end development, Vue 3, as a popular JavaScript framework, has excellent performance and flexibility, providing developers with a wealth of tools and APIs to achieve creative page effects. Among them, custom Transition animation is a very interesting and cool technology. This article will introduce how to learn and apply custom Transition animation in Vue 3 to create cool page effects.

First, let us understand what Transition animation in Vue 3 is. Transition animation is a transition effect implemented in Vue components, which allows components to be displayed in an animated manner when inserted, updated or removed. Vue 3 provides transition components and transition-group components to implement this function. The transition component is used to transition a single element, while the transition-group component can transition multiple elements.

In order to better learn and apply custom Transition animation, we first need to master some basic concepts and techniques. First, we need to understand the transition class names and events in Vue 3. The transition class name includes four states: enter (enter), leave (leave), enter transition (enter-to) and leave transition (leave-to). In the corresponding state, we can achieve animation effects by adding the corresponding class name.

Secondly, it is also very important to understand transition events. Transition events in Vue 3 can be divided into two categories: ordinary events and CSS events. The former includes events such as before-enter, enter, after-enter, before-leave, leave, after-leave, etc., and can perform corresponding operations at different stages of transition. The latter includes enter-active, leave-active and other events, which can be used to add or delete class names at the beginning and end of the animation, thereby triggering CSS animations. By combining transition class names and transition events, we can achieve a variety of custom Transition animation effects.

Next, let us use a simple example to demonstrate how to use Vue 3 to implement custom Transition animation. Suppose we have a button component. When the button is clicked, the button will be shown or hidden with a fade animation effect. First, we need to add the transition component to the template of the button component, and set the transition class name and transition event:

<template>
  <button @click="toggle" class="fade-button">
    {{ visible ? '隐藏' : '显示' }}
  </button>
  <transition name="fade" @before-enter="beforeEnter" @enter="enter" @leave="leave">
    <div v-if="visible" key="content" class="fade-content">
      欢迎来到炫酷的页面效果世界!
    </div>
  </transition>
</template>
Copy after login

In the click event of the button, we need to change a responsive data visible## The value of # is used to switch between showing or hiding the button:

<script>
export default {
  data() {
    return {
      visible: false
    };
  },
  methods: {
    toggle() {
      this.visible = !this.visible;
    },
    beforeEnter(el) {
      el.style.opacity = 0;
    },
    enter(el, done) {
      el.offsetWidth; // 强制重绘
      el.style.opacity = 1;
      done();
    },
    leave(el, done) {
      el.style.opacity = 0;
      setTimeout(done, 1000); // 设置延时以等待动画结束
    }
  }
};
</script>
Copy after login

In the above code, we get the element to be animated through the

el parameter, and implement animation by changing its style Effect. At the end of the enter and leave events, we also notify Vue that the animation has been completed through the done function, so that the next step can be performed.

Finally, we also need to add corresponding CSS styles for these animations. We can define

.fade-enter, .fade-leave, .fade-enter-to and .fade-leave- in the style sheet to class name, and define the transition effect through the transition attribute:

.fade-enter,
.fade-leave {
  opacity: 0;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 1s;
}

.fade-enter-to,
.fade-leave-to {
  opacity: 1;
}
Copy after login
Through the above steps, we successfully implemented a simple custom Transition with fade-in and fade-out effect animation. When the user clicks the button, the content area will be displayed or hidden in a gradient manner, which increases the interactivity and visual effect of the page.

In actual development, we can achieve various customized Transition animation effects by adjusting the transition class name and transition events, and adding corresponding CSS styles according to specific needs. For example, you can achieve more vivid and rich page effects by setting CSS properties such as rotation, scaling, and translation.

To sum up, learning and applying custom Transition animation in Vue 3 can add a very cool and personalized element to our page. By mastering transition class names and transition events, and flexibly using CSS styles, we can achieve rich and diverse page transition effects. As a front-end developer, you must not only be proficient in the basic usage of Vue 3, but also need to continuously learn and explore various innovative page effects to improve user experience and page attractiveness. I wish you all will gain something when learning and applying the custom Transition animation in Vue 3, and create more cool and personalized page effects!

The above is the detailed content of Learn custom Transition animation in Vue 3 to create cool page effects. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

Vue realizes marquee/text scrolling effect Vue realizes marquee/text scrolling effect Apr 07, 2025 pm 10:51 PM

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with &lt;div&gt;. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

How to query the version of vue How to query the version of vue Apr 07, 2025 pm 11:24 PM

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the &lt;script&gt; tag in the HTML file that refers to the Vue file.

How to use vue pagination How to use vue pagination Apr 08, 2025 am 06:45 AM

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()

See all articles