先来看效果图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
最后在看一下效果图: