显示动画:v-enter-active
隐藏动画:.v-leave-active
示例
- <template>
- <div>
- <button @click="isShow = !isShow">点击切换button>
- <transition>
- <h1 v-show="isShow" style="background-color: orange">你好啊!!!h1>
- transition>
- div>
- template>
-
- <script>
- export default {
- name: "Test",
- data() {
- return {
- isShow: true,
- };
- },
- };
- script>
-
- <style scoped>
- .v-enter-active {
- animation: xin 1s linear;
- }
-
- .v-leave-active {
- animation: xin 1s linear reverse;
- }
-
- @keyframes xin {
- 0% {
- transform: translateX(-100%);
- }
-
- 100% {
- transform: translateX(0px);
- }
- }
- style>
transition的name属性能够指定动画
但是动画必须是“名称”-enter-active,或者‘名称’-enter-active
示例
- <template>
- <div>
- <button @click="isShow = !isShow">点击切换button>
- <transition name="word">
- <h1 v-show="isShow" style="background-color: orange">你好啊!!!h1>
- transition>
- div>
- template>
-
- <script>
- export default {
- name: "Test",
- data() {
- return {
- isShow: true,
- };
- },
- };
- script>
-
- <style scoped>
- .word-enter-active {
- animation: xin 1s linear;
- }
-
- .word-leave-active {
- animation: xin 1s linear reverse;
- }
-
- @keyframes xin {
- 0% {
- transform: translateX(-100%);
- }
-
- 100% {
- transform: translateX(0px);
- }
- }
- style>
一开始就显示出现动画需要给transition添加appear属性
transition只起到一个容器的作用,并不会出现在HTML中
transition与过度配合使用实现4.2
- <template>
- <div>
- <button @click="isShow = !isShow">点击切换button>
- <transition name="word" appear>
- <h1 v-show="isShow" style="background-color: orange">你好啊!!!h1>
- transition>
- div>
- template>
-
- <script>
- export default {
- name: "Test",
- data() {
- return {
- isShow: true,
- };
- },
- };
- script>
-
- <style scoped>
- /* 进入的起点,离开的终点 */
- .word-enter,
- .word-leave-to {
- transform: translateX(-100%);
- }
-
- /* 给来到和离开添加过度 */
- .word-enter-active,
- .word-leave-active {
- transition: 0.5s linear;
- }
-
- /* 进入的终点,离开的起点 */
- .word-enter-to,
- .word-leave {
- transform: translateX(0);
- }
- style>
- <template>
- <div>
- <button @click="isShow = !isShow">点击切换button>
- <transition-group name="word" appear>
- <h1 v-show="isShow" key="1" style="background-color: orange">你好啊!!!h1>
- <h1 v-show="!isShow" key="2" style="background-color: orange">你好啊!!!h1>
- transition-group>
- div>
- template>
-
- <script>
- export default {
- name: "Test",
- data() {
- return {
- isShow: true,
- };
- },
- };
- script>
-
- <style scoped>
- /* 进入的起点,离开的终点 */
- .word-enter,
- .word-leave-to {
- transform: translateX(-100%);
- }
-
- /* 给来到和离开添加过度 */
- .word-enter-active,
- .word-leave-active {
- transition: 0.5s linear;
- }
-
- /* 进入的终点,离开的起点 */
- .word-enter-to,
- .word-leave {
- transform: translateX(0);
- }
- style>
- <div>
- <button @click="isShow = !isShow">点击切换button>
-
- <transition-group appear
- name="animate__animated animate__bounce"
- enter-active-class="animate__backInDown"
- leave-active-class="animate__backOutDown"
- >
- <h1 v-show="isShow" key="1" style="background-color: orange">你好啊!!!h1>
- <h1 v-show="!isShow" key="2" style="background-color: orange">你好啊!!!h1>
- transition-group>
- div>
-
- <script>
-
- import 'animate.css';
-
- export default {
- name: "Test",
- data() {
- return {
- isShow: true,
- };
- },
- };
- script>
-
- <style scoped>
- /* 进入的起点,离开的终点 */
- .word-enter,
- .word-leave-to {
- transform: translateX(-100%);
- }
-
- /* 给来到和离开添加过度 */
- .word-enter-active,
- .word-leave-active {
- transition: 0.5s linear;
- }
-
- /* 进入的终点,离开的起点 */
- .word-enter-to,
- .word-leave {
- transform: translateX(0);
- }
- style>
