Home > Web Front-end > Vue.js > A brief analysis of how vue achieves animation effects

A brief analysis of how vue achieves animation effects

青灯夜游
Release: 2023-02-01 19:55:12
forward
2020 people have browsed it

How does vue achieve animation effects? The following article will give you a brief understanding of transitions and animations encapsulated in Vue. I hope it will be helpful to you!

A brief analysis of how vue achieves animation effects

Vue encapsulated transition and animation

1. Function

When inserting, updating, or removing DOM elements, add style class names to the elements when appropriate.

2. Writing method

Prepare the style: the style of the element entry

<template>
  <div>
    <button @click="isShow = !isShow">隐藏展示</button>
    <transition name="hidden" appear>
      <h1 v-show="isShow">隐藏展示</h1>
    </transition>
  </div>
</template>
<script>
export default {
  name: "Test",
  data() {
    return {
      isShow: true
    };
  }
};
</script>
<style scoped>
h1 {
  background-color: orange;
}
.hidden-enter-active {
  animation: shanyu 1s;
}
.hidden-leave-active {
  animation: shanyu 1s reverse;
}
@keyframes shanyu {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0px);
  }
}
</style>
Copy after login

[Related recommendations: vuejs Video tutorialweb front-end development

3. Style

You need to have this animate.css first Oh

npm i animate.css
Copy after login

                                      A brief analysis of how vue achieves animation effects

1.v-enter: the starting point of entry

2.v-enter-active: in the process of entry

3 .v-enter-to: End point of entry

<template>
  <div>
    <button @click="isShow = !isShow">隐藏展示</button>
    <transition-group name="shanyu" appear>
      <h1 v-show="isShow" key = &#39;1&#39;>隐藏展示</h1>
      <h1 v-show="isShow" key = &#39;2&#39;>隐藏展示</h1>
    </transition-group>
  </div>
</template>
<script>
export default {
  name: "Test2",
  data() {
    return {
      isShow: true
    };
  }
};
</script>
<style scoped>
h1 {
  background-color: rgb(139, 37, 255);
}
/* 进入的起点,离开的起点 */
.shanyu-enter,
.shanyu-leave-to {
  transform: translateX(-100%);
}
.shanyu-enter-active,.shanyu-leave-active{
  transition: .5s linear;
}
/* 进入的终点离开的终点 */
.shanyu-enter-to,
.shanyu-leave {
  transform: translateX(0);
}
</style>
Copy after login

Style of element leaving:

1.V-leave: Starting point of departure

2.V-leave-active : During the process of leaving

3.V-leave-to: The end point of leaving

4. Use to wrap the element to be transitioned, and configure the name attribute

<transitionname="hello">
    <h1v-show=" isShow">你好啊! </h1>
</transition>
Copy after login

Note: If there are multiple elements that need to be transitioned, you need to use: , and each element must specify a key value.

    <transition-group name="shanyu" appear>
      <h1 v-show="isShow" key = &#39;1&#39;>隐藏展示</h1>
      <h1 v-show="isShow" key = &#39;2&#39;>隐藏展示</h1>
    </transition-group>
Copy after login

A brief analysis of how vue achieves animation effects

(Learning video sharing: vuejs introductory tutorial, Basic programming video)

The above is the detailed content of A brief analysis of how vue achieves animation effects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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