单元素过渡

appear 初始渲染

通过 appear attribute 设置节点在初始渲染的过渡

  • appear + css

    <transition appear appear-class="custom-appear-class"appear-to-class="custom-appear-to-class" (2.1.8+)appear-active-class="custom-appear-active-class" >
    <!-- ... -->
    </transition>
  • appear + js钩子

    <transition appear v-on:before-appear="customBeforeAppearHook"v-on:appear="customAppearHook" v-on:after-appear="customAfterAppearHook"v-on:appear-cancelled="customAppearCancelledHook" >
    <!-- ... -->
    </transition>

过渡

v-if 或者 v-show 条件显示或者动态组件下可以添加进入/离开过渡

<div id="demo">
<button v-on:click="show = !show">Toggle</button>
<transition name="fade">
<p v-if="show">hello</p>
</transition>
</div>
<script>
new Vue({
el: '#demo',
data: {show: true }
})
</script>
<style>
/* 定义进入 /离开的过渡动作 */
.fade-enter-active, .fade-leave-active {transition: opacity .5s;}
/* .fade-leave-active below version 2.1.8 */
.fade-enter, .fade-leave-to {opacity: 0;}
</style>

动画

CSS 动画用法同 CSS 过渡,区别是在动画中 v-enter 类名在节点插入 DOM 后不会立即删除,而是在 animationend 事件触发时删除。

<div id="demo">
<button v-on:click="show = !show">Toggle</button>
<transition name="bounce">
<p v-if="show">hello</p>
</transition>
</div>
<script>
new Vue({
el: '#demo',
data: {show: true }
})
</script>
<style>
/* 定义进入 / 离开的动画 */
.bounce-enter-active {
animation: bounce-in .5s;
} .bounce-leave-active {
animation: bounce-in .5s reverse;
} @keyframes bounce-in {
0% {transform: scale(0);}
50% {transform: scale(1.5);}
100% {transform: scale(1);}
}
</style>

在一些场景中,你需要给同一个元素同时设置两种过渡动效,比如 animation 很快的被触发并完成了,而 transition 效果还没结束。在这种情况中,你就需要使用 type attribute 并设置 animationtransition 来明确声明你需要 Vue 监听的类型。

多元素过渡

  • 多个原生标签可以使用 v-if/v-else:

    最常见的多标签过渡是一个列表和描述这个列表为空消息的元素:

    <transition>
    <table v-if="items.length > 0">
    <!-- ... -->
    </table>
    <p v-else>Sorry, no items found.</p>
    </transition>
  • 相同标签名的元素切换时,需要通过 key attribute 设置唯一的值

    <transition>
    <button v-if="isEditing" key="save">Save</button>
    <button v-else key="edit">Edit</button>
    </transition>
    <!-- 也可以通过给同一个元素的 key attribute 设置不同的状态 -->
    <transition>
    <button v-bind:key="isEditing">{{ isEditing ? 'Save' : 'Edit' }}</button> </transition>
  • 多个组件的过渡可以使用动态组件:

    <div id="comp">
    <radio v-model="view" value="v-a">A</radio>
    <radio v-model="view" value="v-b">B</radio>
    <transition name="component-fade" mode="out-in">
    <component v-bind:is="view"></component>
    </transition>
    </div>
    <script>
    new Vue({
    el: '#transition-components-demo',
    data: {view: 'v-a'},
    components: {
    'v-a': {
    template: '<div>Component A</div>'
    },
    'v-b': {
    template: '<div>Component B</div>'
    }
    }
    })
    </script>
    <style>
    .comp-fade-enter-active, .comp-fade-leave-active {transition: opacity .3s ease;}
    .comp-fade-enter, .comp-fade-leave-to {opacity: 0;}/* .component-fade-leave-active for below version 2.1.8 */
    </style>
  • 列表过渡

    <div id="list-demo" class="demo">
    <button v-on:click="add">Add</button>
    <button v-on:click="remove">Remove</button>
    <transition-group name="list" tag="p">
    <span v-for="item in items" v-bind:key="item" class="list-item">{{ item }}</span>
    </transition-group>
    </div>
    <script>
    new Vue({
    el: '#list-demo',
    data: {
    items: [1,2,3,4,5,6,7,8,9],
    next: 10
    },
    computed:{
    rIndex: function () {
    return Math.floor(Math.random() * this.items.length)
    },
    },
    methods: {
    add: function () {
    this.items.splice(this.rIndex, 0, this.next++)
    },
    remove: function () {
    this.items.splice(this.rIndex, 1)
    },
    }
    })
    </script>
    <style>
    .list-item {
    display: inline-block;
    margin-right: 10px;
    display: inline-block;
    }
    .list-enter-active, .list-leave-active {
    transition: all 1s;
    }
    .list-enter, .list-leave-to {
    opacity: 0;
    transform: translateY(30px);
    }
    /* .list-leave-active for below version 2.1.8 */
    .list-move {
    transition: transform 1s;
    }
    </style>

多维表格重排

https://codesandbox.io/s/github/vuejs/vuejs.org/tree/master/src/v2/examples/vue-20-list-move-transitions?file=/index.html:0-1157

<!DOCTYPE html>
<html>
<head>
<title>List Move Transitions Sudoku Example</title>
<script src="https://unpkg.com/vue@2"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.1/lodash.min.js"></script>
<link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body>
<div id="sudoku-demo" class="demo">
<h1>Lazy Sudoku</h1>
<p>Keep hitting the shuffle button until you win.</p>
<button @click="shuffle">Shuffle</button>
<transition-group name="cell" tag="div" class="container">
<div v-for="cell in cells" :key="cell.id" class="cell">{{ cell.number }}</div>
</transition-group>
</div>
<script>
new Vue({
el: "#sudoku-demo",
data: {
cells: Array.apply(null, { length: 81 }).map(function(_, index) {
return {
id: index,
number: (index % 9) + 1
};
})
},
methods: {
shuffle: function() {
//创建一个被打乱值的集合
this.cells = _.shuffle(this.cells);
}
}
});
</script>
</body>
</html>

VUE学习-元素过渡的更多相关文章

  1. Vue多元素过渡

    前面的话 前面分别介绍了单元素过渡的CSS过渡和JS过渡,本文将详细介绍Vue多元素过渡 常见示例 最常见的多标签过渡是一个列表和描述这个列表为空消息的元素: <transition> & ...

  2. vue 单元素过渡

    demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  3. 066_末晨曦Vue技术_过渡 & 动画之多个元素的过渡

    多个元素的过渡 点击打开视频讲解更加详细 我们之后讨论多个组件的过渡,对于原生标签可以使用 v-if/v-else.最常见的多标签过渡是一个列表和描述这个列表为空消息的元素: <transiti ...

  4. Vue学习目录

    前面的话 近年来,前端框架发展火热,新的框架和名词不停地出现在开发者眼前,而且开发模式也产生了一定的变化.目前来看,前端MVVM框架的出现给开发者带来了不小的便利,其中的代表就有Angular.js. ...

  5. VUE学习总结

    VUE学习总结 文档:https://cn.vuejs.org/v2/guide/ Webstorm的一些常用快捷键:1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任 ...

  6. vue学习笔记(十)路由

    前言 在上一篇博客vue学习笔记(九)vue-cli中的组件通信内容中,我们学习组件通信的相关内容和进行了一些组件通信的小练习,相信大家已经掌握了vue-cli中的组件通信,而本篇博客将会带你更上一层 ...

  7. Vue学习笔记——Vue-router

    转载:https://blog.csdn.net/guanxiaoyu002/article/details/81116616 第1节:Vue-router入门 .解读router/index.js文 ...

  8. Vue学习笔记-2

    前言 本文非vue教程,仅为学习vue过程中的个人理解与笔记,有说的不正确的地方欢迎指正讨论 1.computed计算属性函数中不能使用vm变量 在计算属性的函数中,不能使用Vue构造函数返回的vm变 ...

  9. Vue学习笔记-1

    前言 本文不是Vue.js的教程,只是一边看官网Vue的教程文档一边记录并总结学习过程中遇到的一些问题和思考的笔记. 1.vue和avalon一样,都不支持VM初始时不存在的属性 而在Angular里 ...

  10. Vue学习记录第一篇——Vue入门基础

    前面的话 Vue中文文档写得很好,界面清爽,内容翔实.但文档毕竟不是教程,文档一上来出现了大量的新概念,对于新手而言,并不友好.个人还是比较喜欢类似于<JS高级程序设计>的风格,从浅入深, ...

随机推荐

  1. [python] 基于matplotlib_venn实现维恩图的绘制

    文章目录 VENN DIAGRAM(维恩图) 1. 具有2个分组的基本的维恩图 Venn diagram with 2 groups 2. 具有3个组的基本维恩图 Venn diagram with ...

  2. python之路36 MySQL查询关键字

    报错及作业讲解 报错 1.粗心大意 单词拼写错误 2.手忙脚乱 不会看报错 思考错误的核心 作业讲解 '''表与表中数据的关系可能会根据业务逻辑的不同 发生改变 不是永远固定的''' 服务器表与应用程 ...

  3. 精华推荐 | 【JVM深层系列】「GC底层调优系列」一文带你彻底加强夯实底层原理之GC垃圾回收技术的分析指南(GC原理透析)

    前提介绍 很多小伙伴,都跟我反馈,说自己总是对JVM这一块的学习和认识不够扎实也不够成熟,因为JVM的一些特性以及运作机制总是混淆以及不确定,导致面试和工作实战中出现了很多的纰漏和短板,解决广大小伙伴 ...

  4. Grafana 系列文章(五):Grafana Explore 查询管理

    ️URL: https://grafana.com/docs/grafana/latest/explore/query-management/ Description: Explore 中的查询管理 ...

  5. 用XAMPP搭建本地:Web服务器,访问服务器,下载服务器。

    用XAMPP搭建本地:Web服务器,访问服务器,下载服务器. 首先需要下载XAMPP,链接为:XAMPP下载地址,XAMPP中文网. 下载完成后进行安装,直接一键点到底. 一.如何确定我们安装完成了? ...

  6. linux环境编程(3): 使用POSIX IPC完成进程间通信

    1. 写在前面 之前的文章总结了使用管道进行进程间通信的方法,除了pipe和fifo,Linux内核还为我们提供了其他更高级的IPC方式,包括共享内存,消息队列,信号量等,本篇文章会通过一个具有完整逻 ...

  7. Vue3 组件之间的数据传递

    1.组件分为:页面级组件和功能组件

  8. JAVA 进阶完结

    1.接口 这个类中 有了新的关键字 abstract 并且里面的方法没有括号 这样的类 上图就是 抽象函数与抽象类 那么继承抽象类的子类需要做什么工作呢 2.代码编写的思路 3.接口 接口跟抽像类的很 ...

  9. 图卷积神经网络分类的pytorch实现

    图神经网络(GNN)目前的主流实现方式就是节点之间的信息汇聚,也就是类似于卷积网络的邻域加权和,比如图卷积网络(GCN).图注意力网络(GAT)等.下面根据GCN的实现原理使用Pytorch张量,和调 ...

  10. 题解 P1627 [CQOI2009] 中位数

    傻逼题但是被自己的傻逼操作爆了好几次零(悲愤 .... 没什么好讲的,一眼题... //SIXIANG #include <iostream> #define int long long ...