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

首頁 > 語言 > JavaScript > 正文

vue如何進行動畫的封裝

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

本文實例為大家分享了vue動畫封裝的具體代碼,供大家參考,具體內容如下

<style>  .v-enter,.v-leave-to{    opacity: 0;  }  .v-enter-active,.v-leave-active{    transition:opacity 1s;  }</style><div id='app'>  <transition>    <div v-if='show'>hello world</div>  </transition>  <button @click='handleClick'>切換</button></div><script>var vm = new Vue({  el:'#app',  data:{    show:true  },  methods:{    handleClick:function(){      this.show = !this.show;    }  }})</script>

有時候這種漸隱漸現的效果用的比較多,要復用,需要封裝一下,怎么封裝呢

<style>  .v-enter,.v-leave-to{    opacity: 0;  }  .v-enter-active,.v-leave-active{    transition:opacity 1s;  }</style><div id='app'>  <fade :show='show'>    <div>hello world</div>  </fade>  <fade :show='show'>    <h1>hello world</h1>  </fade>  <button @click='handleClick'>切換</button></div><script>Vue.component('fade',{  props:['show'],  template: `    <transition>      <slot v-if='show'></slot>    </transition>  `})var vm = new Vue({  el:'#app',  data:{    show:false  },  methods:{    handleClick:function(){      this.show = !this.show;    }  }})</script>

可以這樣封裝,將dom元素傳入slot,除了這樣,還可以樣式一起封裝進去

<div id='app'>  <fade :show='show'>    <div>hello world</div>  </fade>  <fade :show='show'>    <h1>hello world</h1>  </fade>  <button @click='handleClick'>切換</button></div><script>Vue.component('fade',{  props:['show'],  template: `    <transition @before-enter='handleBeforeEnter' @enter='handleEnter'>      <slot v-if='show'></slot>    </transition>  `,  methods:{    handleBeforeEnter:function(el){      el.style.color='red'    },    handleEnter:function(el,done){      setTimeout(()=>{        el.style.color='green';        done();      },2000)    }  }})var vm = new Vue({  el:'#app',  data:{    show:false  },  methods:{    handleClick:function(){      this.show = !this.show;    }  }})</script>

把樣式一起封裝進來,是比較推薦的方式。

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

圖片精選

主站蜘蛛池模板: 屏东市| 永修县| 金坛市| 平乐县| 乌海市| 彩票| 灌阳县| 寿光市| 镇巴县| 克拉玛依市| 杭锦后旗| 柳州市| 临沂市| 崇仁县| 芦溪县| 富民县| 巫溪县| 双峰县| 牡丹江市| 裕民县| 茌平县| 康马县| 靖宇县| 古丈县| 郧西县| 盐亭县| 湘西| 建昌县| 鄂伦春自治旗| 通州市| 仲巴县| 许昌市| 鱼台县| 肇州县| 紫阳县| 平谷区| 连山| 尼玛县| 屏边| 阳原县| 应用必备|