Vue中多個元素或組件的過渡

<!DOCTYPE?html>
<html>
<head>
????<title></title>
????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
????<link?rel="stylesheet"?type="text/css"?href="./animate.css">
????<script?src="./vue.js"></script>
????<!--?<script?src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>?-->
????<style>
????????.myfade-enter,?.v-leave-to?{
????????????opacity:?0;
????????}
????????.v-enter-active,?.v-leave-active?{
????????????transition:?opacity?1s;
????????}
????</style>
</head>
<body>
????<div?id="root">
????????<transition?name="myfade">
????????????<div?v-if="show">hello</div>
????????????<div?v-else>bye</div>
????????</transition>
????????<button?@click="handleClick">切換</button>
????</div>
????<script?type="text/javascript">
????????var?vm?=?new?Vue({
????????????el:?"#root",
????????????data:?{
????????????????show:?true
????????????},
????????????methods:?{
????????????????handleClick:?function()?{
????????????????????this.show?=?!this.show
????????????????}
????????????}
????????});
????</script>
</body>
</html>

(像上面這種加了style并不會實現(xiàn)漸變效果,因為vue默認是會盡量復(fù)用dom,想要vue不復(fù)用dom,要給其加上不同的key值)

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)尼金平免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

加上不同key值后,漸變效果有了:

<!DOCTYPE?html>
<html>
<head>
????<title></title>
????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
????<link?rel="stylesheet"?type="text/css"?href="./animate.css">
????<script?src="./vue.js"></script>
????<!--?<script?src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>?-->
????<style>
????????.myfade-enter,?.myfade-leave-to?{
????????????opacity:?0;
????????}
????????.myfade-enter-active,?.myfade-leave-active?{
????????????transition:?opacity?1s;
????????}
????</style>
</head>
<body>
????<div?id="root">
????????<transition?name="myfade">
????????????<div?v-if="show"?key="hello">hello</div>
????????????<div?v-else?key="bye">bye</div>
????????</transition>
????????<button?@click="handleClick">切換</button>
????</div>
????<script?type="text/javascript">
????????var?vm?=?new?Vue({
????????????el:?"#root",
????????????data:?{
????????????????show:?true
????????????},
????????????methods:?{
????????????????handleClick:?function()?{
????????????????????this.show?=?!this.show
????????????????}
????????????}
????????});
????</script>
</body>
</html>

如果想設(shè)置多個屬性之間的切換效果,可以用mode(mode="in-out":先顯示要顯示的再隱藏要隱藏的。mode="out-in":和前面的相反):

<!DOCTYPE?html>
<html>
<head>
????<title></title>
????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
????<link?rel="stylesheet"?type="text/css"?href="./animate.css">
????<script?src="./vue.js"></script>
????<!--?<script?src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>?-->
????<style>
????????.myfade-enter,?.myfade-leave-to?{
????????????opacity:?0;
????????}
????????.myfade-enter-active,?.myfade-leave-active?{
????????????transition:?opacity?1s;
????????}
????</style>
</head>
<body>
????<div?id="root">
????????<transition?name="myfade"?mode="out-in">
????????????<div?v-if="show"?key="hello">hello</div>
????????????<div?v-else?key="bye">bye</div>
????????</transition>
????????<button?@click="handleClick">切換</button>
????</div>
????<script?type="text/javascript">
????????var?vm?=?new?Vue({
????????????el:?"#root",
????????????data:?{
????????????????show:?true
????????????},
????????????methods:?{
????????????????handleClick:?function()?{
????????????????????this.show?=?!this.show
????????????????}
????????????}
????????});
????</script>
</body>
</html>

組件動畫也是可以的(不需要上面的不同值的key):

<!DOCTYPE?html>
<html>
<head>
????<title></title>
????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
????<link?rel="stylesheet"?type="text/css"?href="./animate.css">
????<script?src="./vue.js"></script>
????<!--?<script?src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>?-->
????<style>
????????.myfade-enter,?.myfade-leave-to?{
????????????opacity:?0;
????????}
????????.myfade-enter-active,?.myfade-leave-active?{
????????????transition:?opacity?1s;
????????}
????</style>
</head>
<body>
????<div?id="root">
????????<transition?name="myfade"?mode="out-in">
????????????<child1?v-if="show">hello</child1>
????????????<child2?v-else>bye</child2>
????????</transition>
????????<button?@click="handleClick">切換</button>
????</div>
????<script?type="text/javascript">
????????Vue.component("child1",?{
????????????template:?"<div>child1</div>"
????????});
????????Vue.component("child2",?{
????????????template:?"<div>child2</div>"
????????});
????????var?vm?=?new?Vue({
????????????el:?"#root",
????????????data:?{
????????????????show:?true
????????????},
????????????methods:?{
????????????????handleClick:?function()?{
????????????????????this.show?=?!this.show
????????????????}
????????????}
????????});
????</script>
</body>
</html>

動態(tài)組件的實現(xiàn)方法:

<!DOCTYPE?html>
<html>
<head>
????<title></title>
????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
????<link?rel="stylesheet"?type="text/css"?href="./animate.css">
????<script?src="./vue.js"></script>
????<!--?<script?src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>?-->
????<style>
????????.myfade-enter,?.myfade-leave-to?{
????????????opacity:?0;
????????}
????????.myfade-enter-active,?.myfade-leave-active?{
????????????transition:?opacity?1s;
????????}
????</style>
</head>
<body>
????<div?id="root">
????????<transition?name="myfade"?mode="out-in">
????????????//通過動態(tài)組件的方式實現(xiàn):
????????????<component?:is="type"></component>
????????</transition>
????????<button?@click="handleClick">切換</button>
????</div>
????<script?type="text/javascript">
????????Vue.component("child1",?{
????????????template:?"<div>child1</div>"
????????});
????????Vue.component("child2",?{
????????????template:?"<div>child2</div>"
????????});
????????var?vm?=?new?Vue({
????????????el:?"#root",
????????????data:?{
????????????????type:?"child1"
????????????},
????????????methods:?{
????????????????handleClick:?function()?{
????????????????????this.type?=?this.type?==?"child1"???"child2"?:?"child1"
????????????????}
????????????}
????????});
????</script>
</body>
</html>

網(wǎng)站名稱:Vue中多個元素或組件的過渡
網(wǎng)頁路徑:http://muchs.cn/article22/jchocc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計公司移動網(wǎng)站建設(shè)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司