当前位置:网站首页>12 【nextTick 过渡与动画】
12 【nextTick 过渡与动画】
2022-07-31 05:10:00 【DSelegent】
24.nextTick
这是一个生命周期钩子
- 语法:
this.$nextTick(回调函数) - 作用:在下一次 DOM 更新结束后执行其指定的回调。
- 什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。
比如编辑按钮使文本变成表单且自动获取焦点
点击表单时会用一个布尔值配合v-show使表单显示,可是改变布尔值的时候,后面的focus方法会跟着执行,然后再渲染模板
<template>
<li>
<label>
<input type="checkbox" :checked="todo.done" >
<span v-show="!todo.isEdit">{
{ todo.title }}</span>
<input type="text" v-show="todo.isEdit" :value="todo.title"ref="inputTitle"/>
</label>
<button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">
编辑
</button>
</li>
</template>
<script> export default {
name: "MyItem", props: ["todo"], // 声明接收todo methods: {
handleEdit(todo) {
// 编辑 if (todo.hasOwnProperty("isEdit")) {
todo.isEdit = true; } else {
this.$set(todo, "isEdit", true); } this.$nextTick(function () {
this.$refs.inputTitle.focus(); }); }, }, }; </script>
25.过渡与动画
25.1基本介绍
Vue 在插入、更新或者移除 DOM 时,提供多种不同方式的应用过渡效果。
包括以下工具:
1、在 CSS 过渡和动画中自动应用 class;
2、配合使用第三方 CSS 动画库,如 Animate.css;
3、在过渡钩子函数中使用 JavaScript 直接操作 DOM;
4、配合使用第三方 JavaScript 动画库,如 Velocity.js。

作用:Vue封装的在插入、更新或移除
DOM元素时,在合适的时候给元素添加样式类名。写法:
准备好样式:
- 元素进入的样式:
- v-enter:进入的起点
- v-enter-active:进入过程中
- v-enter-to:进入的终点
- 元素离开的样式:
- v-leave:离开的起点
- v-leave-active:离开过程中
- v-leave-to:离开的终点
- 元素进入的样式:
使用
<transition>包裹要过度的元素,并配置name属性,此时需要将上面样式名的v换为name<transition name="hello"> <h1 v-show="isShow">你好啊!</h1> </transition>要让页面一开始就显示动画,需要添加
appear备注:若有多个元素需要过度,则需要使用:
<transition-group>,且每个元素都要指定key值。<transition-group name="hello" appear> <h1 v-show="!isShow" key="1">你好啊!</h1> <h1 v-show="isShow" key="2">尚硅谷!</h1> </transition-group>第三方动画库
Animate.css<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">你好啊!</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>
25.2动画的使用
<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>
<style>
.hello-enter-active{
animation: hello 0.5s linear;
}
.hello-leave-active{
animation: hello 0.5s linear reverse;
}
@keyframes hello {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>
25.3过渡的使用
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition-group name="hello" appear>
<h1 v-show="!isShow" key="1">你好啊!</h1>
<h1 v-show="isShow" key="2">尚硅谷!</h1>
</transition-group>
</div>
</template>
<script>
export default {
name:'Test',
data() {return {isShow:true}},
}
</script>
<style scoped>
h1 {
background-color: orange;
/* transition: 0.5s linear; */
}
/* 进入的起点、离开的终点 */
.hello-enter,.hello-leave-to {
transform: translateX(-100%);
}
.hello-enter-active,.hello-leave-active{
transition: 0.5s linear;
}
/* 进入的终点、离开的起点 */
.hello-enter-to,.hello-leave {
transform: translateX(0);
}
</style>
边栏推荐
- 【一起学Rust】Rust的Hello Rust详细解析
- 实验8 DNS解析
- Element concatenation operations in numpy and pytorch: stack, concatenat, cat
- 【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
- 剑指offer基础版 ----- 第28天
- 剑指offer专项突击版 ---第 5 天
- 数据库上机实验6 数据库完整性
- 目标检测学习笔记
- 第7章 网络层第3次练习题答案(第三版)
- If the account number or password is entered incorrectly for many times, the account will be banned.
猜你喜欢

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it

Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

剑指offer基础版 --- 第24天

实验8 DNS解析

MySQL(更新中)

【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级

Why use Flink and how to get started with Flink?

面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式

Refinement of the four major collection frameworks: Summary of List core knowledge
随机推荐
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
Qt Creator + CMake 运行调试总会自动 build 所有目标
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
Unity mobile game performance optimization series: performance tuning for the CPU side
Flask-based three-party login process
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
Flask 的初识
面试官,不要再问我三次握手和四次挥手
可点击也可直接复制指定内容js
uni-app进阶之生命周期【day8】
C语言实验五 循环结构程序设计(二)
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
解决响应式数据依赖响应式数据无响应问题
如何将项目部署到服务器上(全套教程)
账号或密码多次输入错误,进行账号封禁
Anaconda configure environment directives
C语言如何分辨大小端
闭包(四)----IIFE