富贵资源网 Design By www.hznty.com
常见示例
最常见的多标签过渡是一个列表和描述这个列表为空消息的元素:
<transition> <table v-if="items.length > 0"> <!-- ... --> </table> <p v-else>Sorry, no items found.</p> </transition>
下面是一个例子
<style> .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition:opacity .5s;} </style> <div id="demo"> <button @click="clear">清空数据</button> <button @click="reset">重置</button> <transition name="fade"> <ul v-if="items.length > 0"> <li v-for="item in items">{{item}}</li> </ul> <p v-else>Sorry, no items found.</p> </transition> </div> <script> new Vue({ el: '#demo', data: { items: ['html','css','js'] }, methods:{ clear(){ this.items.splice(0); }, reset(){ history.go(); } } }) </script>
同标签名称
如果是相同标签名的元素切换时,Vue 为了效率只会替换相同标签内部的内容
<style> .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition:opacity .5s;} </style> <div id="demo"> <button @click="show = !show">toggle</button> <transition name="fade"> <p v-if="show">Jb51</p> <p v-else>JB51</p> </transition> </div> <script> new Vue({ el: '#demo', data: { show:true }, }) </script>
由下面的示例可知,两个相同的p元素切换时,无过渡效果
因此,对于具有相同标签名的元素切换的情况,需要通过 key 特性设置唯一的值来标记以让 Vue 区分它们
<div id="demo"> <button @click="show = !show">toggle</button> <transition name="fade"> <p v-if="show" key="trueMatch">Jb51</p> <p v-else key="falseMatch">JB51</p> </transition> </div>
替代if
在一些场景中,可以给通过给同一个元素的 key 特性设置不同的状态来代替 v-if 和 v-else
<transition> <button v-if="isEditing" key="save">Save</button> <button v-else key="edit">Edit</button> </transition>
上面的例子可以重写为
<transition> <button v-bind:key="isEditing"> {{ isEditing "htmlcode"><style> .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition:opacity .5s;} </style> <div id="demo"> <button @click="isEditing = !isEditing">toggle</button> <transition name="fade"> <p v-bind:key="isEditing"> {{ isEditing "htmlcode"><transition> <button v-if="docState === 'saved'" key="saved">Edit</button> <button v-if="docState === 'edited'" key="edited">Save</button> <button v-if="docState === 'editing'" key="editing">Cancel</button> </transition>可以重写为
<transition> <button v-bind:key="docState">{{ buttonMessage }}</button> </transition> computed: { buttonMessage: function () { switch (this.docState) { case 'saved': return 'Edit' case 'edited': return 'Save' case 'editing': return 'Cancel' } } }下面是一个例子
<style> .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition:opacity .5s;} </style> <div id="demo"> <button @click="change">change</button> <transition name="fade"> <p v-bind:key="docState">{{ message }}</p> </transition> </div> <script> new Vue({ el: '#demo', data: { index:0, isEditing:true, arr:['saved','edited','editing'] }, computed: { docState(){ return this.arr[this.index]; }, message() { switch (this.docState) { case 'saved': return 'Edit' case 'edited': return 'Save' case 'editing': return 'Cancel' } } }, methods:{ change(){ this.index = (++this.index)%3; } } }) </script>过渡模式
先看下面这个例子
<style> .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition:opacity .5s;} </style> <div id="demo"> <transition name="fade"> <button :key="isOn" @click="isOn = !isOn">{{ isOn "htmlcode"><div id="demo"> <transition name="fade" mode="in-out"> <button :key="isOn" @click="isOn = !isOn">{{ isOn "htmlcode"><div id="demo"> <transition name="fade" mode="out-in"> <button :key="isOn" @click="isOn = !isOn">{{ isOn "htmlcode"><style> #demo{position:relative;} #demo button{position:absolute;left:40px;} .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition: 1s;} </style> <div id="demo"> <transition name="fade" > <button :key="isOn" @click="isOn = !isOn">{{ isOn "htmlcode"><style> #demo{position:relative;} #demo button{position:absolute;left:40px;} .fade-enter,.fade-leave-to{opacity:0;} .fade-enter{transform:translateX(30px);} .fade-leave-to{transform:translateX(-30px);} .fade-enter-active,.fade-leave-active{transition: 1s;} </style>如果设置in-out模式,将实现更酷的滑动效果
<style> #demo{position:relative;} #demo button{position:absolute;left:40px;} .fade-enter,.fade-leave-to{opacity:0;} .fade-enter{transform:translateX(30px);} .fade-leave-to{transform:translateX(-30px);} .fade-enter-active,.fade-leave-active{transition: 1s;} </style> <div id="demo"> <transition name="fade" mode="in-out"> <button :key="isOn" @click="isOn = !isOn">{{ isOn "htmlcode"><style> .fade-enter,.fade-leave-to{opacity:0;} .fade-enter-active,.fade-leave-active{transition: .5s;} </style> <div id="example"> <button @click="change">切换页面</button> <transition name="fade" mode="out-in"> <component :is="currentView"></component> </transition> </div> <script> new Vue({ el: '#example', data:{ index:0, arr:[ {template:`<div>ComponentA</div>`}, {template:`<div>ComponentB</div>`}, {template:`<div>ComponentC</div>`} ], }, computed:{ currentView(){ return this.arr[this.index]; } }, methods:{ change(){ this.index = (++this.index)%3; } } })更多关于Vue过渡动画的文章大家可以查看下面的相关链接
富贵资源网 Design By www.hznty.com广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!富贵资源网 Design By www.hznty.com暂无评论...