先來看效果圖html
github:vue
https://github.com/imfing/vuexlearnwebpack
碼雲gitee:git
https://gitee.com/stackfing/vuexlearn 歡迎 stargithub
開始以前您須要有 vue 基礎,以及安裝好 vue-cliweb
新建 vue 項目:vue init webpack vuexlearn
記住安裝的時候須要選擇 vue-routervue-router
進入 vuexlearn 目錄以後安裝 vuex: 這裏使用 npm 安裝 npm install vuex --save
您也能夠使用其餘方式安裝,具體請參考 vuex 官方文檔。vuex
在安裝好 vuex 以後,您就能夠使用 npm run dev
命令運行您的 web 應用了。vue-cli
如今在 main.js
文件中引入 vuexnpm
import vuex from 'vuex' Vue.use(vuex);
在下方添加咱們的 vuex 狀態樹
var store = new vuex.Store({//store對象 state: { states: 'turn-on' }, mutations: { setTransition(state, states) { state.states = states } } })
state.states
就是用來記錄咱們目前的切換狀態, turn-on
爲頁面入棧,turn-off
是頁面出棧。
setTransition(state, states)
方法用來設置 states 的值,在須要的時候咱們會調用它。
接下來,咱們新建一個 common
組件,做爲咱們的做標題欄
<template> <div class="header"> <div class="left" @click="back"> back </div> <div class="center"> {{title}} </div> </div> </template> <script> export default { data() { return {}; }, props: ["title"], methods: { back() { this.$store.commit("setTransition", "turn-off"); this.$router.back(-1); } } }; </script> <style scoped> .header { position: fixed; width: 100%; height: 40px; line-height: 40px; background-color: rgb(100, 231, 60); } .clearfix { overflow: auto; } .left { position: fixed; left: 0px; width: 60px; } .center { left: 50%; position: fixed; } </style>
這裏經過 props
拿到 name
的值,渲染在標題欄上
這裏的切換核心就是在點擊返回的時候,設置整個頁面的動畫效果
新建 4 個頁面,其餘的頁面雷同,因此這裏只貼出一個頁面
<template> <div class="A"> <common title="a"></common> <div class="bottom"> bottom </div> </div> </template> <script> import common from "./common"; export default { data() { return {}; }, components: { common } }; </script> <style scoped> .A { width: 100%; height: 100%; background-color: rgb(214, 198, 52); position: fixed; } .bottom { width: 100%; height: 50px; background-color: red; position: fixed; bottom: 0px; } </style>
<template> <div id="app"> <router-link to="/A" @click.native="clickLink">A</router-link> <router-link to="/B" @click.native="clickLink">B</router-link> <router-link to="/C" @click.native="clickLink">C</router-link> <router-link to="/D" @click.native="clickLink">D</router-link> <transition :name="$store.state.states"> <router-view/> </transition> <div>Index Page</div> </div> </template> <script> export default { name: "App", data() { return { }; }, methods: { clickLink() { this.$store.commit("setTransition", "turn-on"); } }, mounted() { var _this = this; window.addEventListener( "popstate", function(e) { _this.$store.commit("setTransition", "turn-off"); }, false ); } }; </script> <style> * { margin: 0; padding: 0; } .btn { width: 50%; } html, body, #app { height: 100%; } .turn-on-enter { transform: translate3d(100%, 0, 0); } .turn-on-leave-to { /* transform: translate3d(-20%, 0, 0); */ } .turn-on-enter-active, .turn-on-leave-active { transition: transform 0.4s ease; } .turn-off-enter { /* transform: translate3d(-20%, 0, 0); */ } .turn-off-leave-to { transform: translate3d(100%, 0, 0); } .turn-off-enter-active, .turn-off-leave-active { transition: transform 0.4s ease; } .turn-off-leave-active { z-index: 2; } </style>
切換效果就在這裏定義了,經過 vuex 全局保存變量達到頁面入棧、出棧的動畫效果。
github:
https://github.com/imfing/vuexlearn
碼雲gitee:
https://gitee.com/stackfing/vuexlearn
最後在看一下效果圖: