Vue 动画效果、过渡效果

news/2024/7/10 3:05:58 标签: vue

文章目录

  • 动画效果
  • 过渡效果
    • 单个元素
    • 多个元素
  • Animate.css
  • 总结
  • todolist 增加动画效果

动画效果

在这里插入图片描述

新建 Test.vue

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <transition>
      <h1 v-show="isShow">你好 Vue</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: "Test",
  data() {
    return {
      isShow: true
    }
  }
}
</script>

<style scoped>
h1 {
  background-color: orange;
}

.v-enter-active{
  animation: myAni 1s;
}

.v-leave-active{
  animation: myAni 1s reverse;
}

@keyframes myAni {
  from{
    transform: translateX(-100%);
  }
  to{
    transform: translateX(0px);
  }
}
</style>

App.vue

<template>
  <div>
    <Test/>
  </div>
</template>

<script>
//引入组件
import Test from "./components/Test";

export default {
  name: 'App',
  components: {
    Test
  }
}
</script>

<style>
</style>

注意点
1、如果给 transition 标签增加了 name 属性

<transition name="hello">
	<h1 v-show="isShow">你好 Vue</h1>
</transition>

那么动画过渡类名也需要修改:

.hello-enter-active{
  animation: myAni 1s;
}

.hello-leave-active{
  animation: myAni 1s reverse;
}

2、如果想让程序一执行就执行一次动画,那么需要增加 appear

注意 appear 前要有冒号,不写冒号就相当于一个普通属性,值是字符串是 “true”

<transition :appear="true">
      <h1 v-show="isShow">你好 Vue</h1>
    </transition>

或者简写

<transition>
      <h1 v-show="isShow">你好 Vue</h1>
    </transition>

过渡效果

单个元素

复制一份 Test.vue 重命名为 Test2.vue,记得 App 中注册使用

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <transition appear>
      <h1 v-show="isShow">你好 Vue</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: "Test",
  data() {
    return {
      isShow: true
    }
  }
}
</script>

<style scoped>
h1 {
  background-color: orange;
  /*transition:  0.5s linear;*/
}

/*进入的起点,离开的终点*/
.v-enter, .v-leave-to {
  transform: translateX(-100%);
}
/*进入的过程,离开的过程*/
.v-enter-active, .v-leave-active {
  transition: 0.5s linear;
}
/*进入的终点,离开的起点*/
.v-enter-to, .v-leave {
  transform: translateX(0);
}
</style>

在这里插入图片描述

多个元素

在这里插入图片描述

<transition-group>
      <h1 v-show="isShow" key="1">你好 Vue</h1>
      <h1 v-show="!isShow" key="2">闭关修炼 沉迷学习</h1>
    </transition-group>

Animate.css

根据 官网 的使用教程安装、引入、使用即可

1、复制一个 Test2.vue 重命名为 Test3.vue,并在 App 中引入使用
2、安装 Animate.css,运行 npm install animate.css执行安装
3、Test3.vue 中引入,import 'animate.css';
4、transition-group 标签中增加 name="animate__animated animate__bounce"
5、增加一个动画,例如我们增加一个进入动画 enter-active-class="animate__swing",再增加一个离开动画

其中官网右侧列出了动画名,点击可查看效果,同时后边可以复制动画名
在这里插入图片描述
Test3.vue 完整代码

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <transition-group
        appear
        name="animate__animated animate__bounce"
        enter-active-class="animate__swing"
        leave-active-class="animate__backOutUp"
        >
      <h1 v-show="isShow" key="1">你好 Vue</h1>
      <h1 v-show="!isShow" key="2">闭关修炼 沉迷学习</h1>
    </transition-group>
  </div>
</template>

<script>
import 'animate.css';
export default {
  name: "Test",
  data() {
    return {
      isShow: true
    }
  }
}
</script>

<style scoped>
h1 {
  background-color: orange;
}
</style>

查看效果:

在这里插入图片描述

总结

Vue 封装的过渡与动画
1.作用:在插入、更新或移除DOM元素时,在合适的时候给元素添加样式类名
2.图示:
在这里插入图片描述

摘自 Vue 官网

3.写法:
1)准备好样式:

  • 元素进入的样式:
    1.v-enter进入的起点
    2.v-enter-active进入过程中
    3.v-enter-to进入的终
  • 元素离开的样式:
    1.v-leave离开的起点
    2.v-leave-active离开过程中
    3.v-leave-to离开的终点

2)使用<transition>包裹要过度的元素,并配置name属性:<transition name="hello">(可选)

3.备注:若有多个元素需要过度,则需要使用<transition-group>,且每个元读都要指定 key 值

todolist 增加动画效果

现在给增加和删除增加动画效果,所以修改 Item.vue<transition>标签包裹,然后增加动画即可

<template>
  <transition name="todo" appear>
	......
    </li>
  </transition>
</template>

<script>
......
</script>

<style scoped>
......

.todo-enter-active {
  animation: myAni 1s;
}

.todo-leave-active {
  animation: myAni 1s reverse;
}

@keyframes myAni {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0px);
  }
}
</style>

查看效果
在这里插入图片描述
当然也可以把样式加到 List.vue 中,修改一下:


http://www.niftyadmin.cn/n/1816730.html

相关文章

Command 模式 Step by Step

摘要: 提起Command模式&#xff0c;我想没有什么比一个遥控器的例子更能说明问题了&#xff0c;本文将对Command模式作以简单介绍&#xff0c;我们通过完成这个范例来一步步实现GOF的Command模式。 阅读全文张子阳. 2007-12-20 08:17 发表评论转载于:https://www.cnblogs.com/…

AMD,CMD,UMD 三种模块规范 写法格式

一下三块均以 foo.js 为示例文件名&#xff0c;以 jQuery&#xff0c;underscore 为需求组件 ADM&#xff1a;异步模块规范&#xff0c; RequireJs 的支持格式 1 // 文件名: foo.js2 define([jquery, underscore], function ($, _) {3 // 方法4 function a(){}; // 私有方法&a…

Vue 配置代理

文章目录方式一方式二vue 脚手架配置代理总结方式一 首先安装 axios&#xff1a;npm i axios 修改 App.vue <template><div><button click"getBookInfo">获取书籍信息</button></div> </template><script> import axio…

JS中的继承方式总结

1. 原型链继承(又称类继承) Child.prototype new Parent(); 1 function Parent (name, age) {2 this.name name;3 this.age age;4 }5 Parent.prototype.say function(){6 console.log(hello, my name is this.name);7 };8 function Child() {9 } 10 Child.p…

回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。

回发或回调参数无效。在配置中使用 <pages enableEventValidation"true"/> 或在页面中使用 <% Page EnableEventValidation"true" %> 启用了事件验证。出于安全目的&#xff0c;此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服…

Vue github用户搜索案例

文章目录完成样式请求数据完善使用 vue-resource完成样式 1、public 下新建 css 文件夹&#xff0c;放入下载好的 bootstrap.css&#xff0c;并在 index.html 中引入 2、新建 Search.vue <template><section class"jumbotron"><h3 class"jumbo…

js 常用排序整理

排序&#xff1a;   1. 内部排序&#xff1a;     (1). 交换排序&#xff1a;       1). 冒泡排序 稳定         一次比较相邻两个元素的大小&#xff0c;顺序错误的&#xff0c;将其位置互换         &#xff08;从高位到低位 或者 从低位到高位…

学习WPF: 创建数据绑定目录树

如果使用了WPF而不使用数据绑定(手工在界面和数据间进行同步),总会感觉不值.但是大部分讨论WPF数据绑定的文章,主题大多集中在ListBox这样平坦的数据集合上,讲如何绑定层次结构数据的比较少,这里我就通过一个简单的显示磁盘目录树的例子来展示如何完成这样的任务.第一步,当然是…