最近學習了Vue Transition的用法,感覺這個地方知識點挺多的,而且很重要,所以,今天添加一點小筆記
官方文檔:https://cn.vuejs.org/v2/guide/transitions.html
演示地址:http://www.coderlife.com (請在移動端查看,PC端查看請打開移動端調試模式)
前言
看了挺多Vue的UI框架都不帶過渡動畫,今天心血來潮,就把自己平時用的動效抽離出來。可直接通過腳手架init模版配合其他UI框架使用,不需要另外進行配置。
原理
模版中使用了Vue提供的封裝組件 transition,配合CSS類名在 enter/leave 的六種不同的狀態過渡中切換。
對于這些在 enter/leave 過渡中切換的類名,v- 是這些類名的前綴。使用 <transition name="my-transition">
可以重置前綴,比如 v-enter
替換為 my-transition-enter
。
重寫跳轉函數
// 根據具體的跳轉類型更改跳轉屬性值,執行不同的動畫 const nextDirection = (direction) => { let el = document.getElementById('app') if (el) el.setAttribute('transition-direction', direction) } router['_push'] = router['push'] // 重寫路由跳轉方法,設置跳轉類型后跳轉 router.forward = router['push'] = (target) => { nextDirection('forward') setTimeout(() => { router['_push'](target) }) } // 重寫路由返回方法,設置跳轉類型后跳轉到上一頁 router.back = (target) => { nextDirection('back') if (target) { setTimeout(() => { router['_push'](target) }) } history.go(-1) }
How to use
# init template vue init CoderLQChou/v-transition-template my-transition-app # cd project cd my-transition-app # install dependencies npm install # serve with hot reload at localhost:8080 npm run dev # build for production with minification npm run build
倉庫地址:https://github.com/CoderLQChou/vue-transition-template
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com