国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue中多個元素、組件的過渡及列表過渡的方法示例

2024-05-06 15:42:28
字體:
來源:轉載
供稿:網友

多個元素之間過渡動畫效果

多元素之間如何實現過渡動畫效果呢?看下面代碼

.fade-enter,.fade-leave-to{  opacity: 0;}.fade-enter-active,.fade-leave-active{  transition: opacity 3s;}<div id="root">  <transition name="fade">    <div v-if="show">hello world</div>    <div v-else>bye world</div>  </transition>  <button @click="handleClick">切換</button></div>let vm = new Vue({  el: '#root',  data: {    show: true  },  methods: {    handleClick() {      this.show = !this.show    }  }})

這么寫行不行呢?肯定是不行的,因為 Vue 在兩個元素進行切換的時候,會盡量復用dom,就是因為這個原因,導致現在動畫效果不會出現。

如果不讓 Vue 復用dom的話,應該怎么做呢?只需要給這兩個div不同的key值就行了

<div v-if="show" key="hello">hello world</div><div v-else key="bye">bye world</div>

這個時候當div元素進行切換的時候,就不會復用了。

mode

Vue 提供了一mode屬性,來實現多個元素切換時的效果

mode取值in-out,動畫效果是先出現在隱藏

<div id="root">  <transition name="fade" mode="in-out">  //第一次點擊時,執行順序為:①②    <div v-if="show">hello world</div> //再消失 ②    <div v-else>bye world</div>    //先顯示 ①  </transition>  <button @click="handleClick">切換</button>  </div>

mode取值為out-in,動畫效果為先隱藏在出現

<div id="root">  <transition name="fade" mode="out-in">  //第一次點擊時,執行順序為:①②    <div v-if="show">hello world</div> //先消失 ①    <div v-else>bye world</div>    //再顯示 ②  </transition>  <button @click="handleClick">切換</button>  </div>

多個組件之間過渡動畫效果

這里需要借助動態組件來實現多組件之間過渡動畫效果

先用普通的方式來實現切換:

.fade-enter,.fade-leave-to{  opacity: 0;}.fade-enter-active,.fade-leave-active{  transition: opacity 1s;}<div id="root">  <transition name="fade" mode="in-out">    <child-one v-if="show"></child-one>    <child-two v-else></child-two>  </transition>  <button @click="handleClick">切換</button></div>Vue.component('child-one',{  template:'<div>child-one</div>'})Vue.component('child-two',{  template:'<div>child-two</div>'})let vm = new Vue({  el: '#root',  data: {    show: true  },  methods: {    handleClick() {      this.show = !this.show    }  }})

你會發現,這樣子實現組件切換,transition動畫效果是存在的,但是我們想要用動態組件來實現,該怎么弄呢?

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 大姚县| 黄平县| 教育| 浙江省| 孝义市| 蕉岭县| 治县。| 湘潭县| 乐安县| 新建县| 大竹县| 盐边县| 西昌市| 临沂市| 江城| 东源县| 额敏县| 宿州市| 台山市| 盐池县| 宁津县| 诸暨市| 上栗县| 安福县| 西平县| 锡林郭勒盟| 昭觉县| 通许县| 靖江市| 曲松县| 开封市| 城口县| 吐鲁番市| 东平县| 嘉兴市| 临高县| 花垣县| 深水埗区| 敦化市| 白水县| 汉源县|