Vue Transition 實現類原生組件跳轉過渡動畫

官方文檔:https://cn.vuejs.org/v2/guide...html

演示地址:http://www.coderlife.com (請在移動端查看,PC端查看請打開移動端調試模式)vue

前言

看了挺多Vue的UI框架都不帶過渡動畫,今天心血來潮,就把本身平時用的動效抽離出來。可直接經過腳手架init模版配合其餘UI框架使用,不須要另外進行配置。git

原理

模版中使用了Vue提供的封裝組件 transition,配合CSS類名在 enter/leave 的六種不一樣的狀態過渡中切換。
過渡狀態圖示github

對於這些在 enter/leave 過渡中切換的類名,v- 是這些類名的前綴。使用 <transition name="my-transition"> 能夠重置前綴,好比 v-enter 替換爲 my-transition-enternpm

重寫跳轉函數

// 根據具體的跳轉類型更改跳轉屬性值,執行不一樣的動畫
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/CoderLQCho... 歡迎starbash

相關文章
相關標籤/搜索