|
| 1 | +<template> |
| 2 | + <div class="vue-route-transition"> |
| 3 | + <transition :name="state.pageDirection" @leave="setRouterMap"> |
| 4 | + <keep-alive |
| 5 | + v-if="this.keepAlive === true && $route.meta.keepAlive !== false" |
| 6 | + > |
| 7 | + <router-view></router-view> |
| 8 | + </keep-alive> |
| 9 | + <router-view v-else></router-view> |
| 10 | + </transition> |
| 11 | + </div> |
| 12 | +</template> |
| 13 | +<script> |
| 14 | +var localSessionRouteChain = sessionStorage.getItem("$$routeChain") || []; |
| 15 | + |
| 16 | +export default { |
| 17 | + name: "vue-route-transition", |
| 18 | + props: { |
| 19 | + keepAlive: { |
| 20 | + type: Boolean, |
| 21 | + default: true |
| 22 | + } |
| 23 | + }, |
| 24 | + data: function() { |
| 25 | + try { |
| 26 | + localSessionRouteChain = |
| 27 | + this.$route.path !== "/" ? JSON.parse(localSessionRouteChain) : []; |
| 28 | + } catch (error) { |
| 29 | + localSessionRouteChain = []; |
| 30 | + } |
| 31 | + return { |
| 32 | + state: { |
| 33 | + addCount: localSessionRouteChain.length, |
| 34 | + routerMap: {}, |
| 35 | + pageDirection: "fade", |
| 36 | + routeChain: localSessionRouteChain |
| 37 | + } |
| 38 | + }; |
| 39 | + }, |
| 40 | + methods: { |
| 41 | + addRouteChain(route) { |
| 42 | + if (this.state.addCount === 0 && localSessionRouteChain.length > 0) { |
| 43 | + // 排除刷新的时候 |
| 44 | + this.state.addCount = 1; |
| 45 | + } else { |
| 46 | + if ( |
| 47 | + (this.state.addCount !== 0 && |
| 48 | + this.state.routeChain[this.state.routeChain.length - 1].path !== |
| 49 | + route.path) || |
| 50 | + this.state.addCount === 0 |
| 51 | + ) { |
| 52 | + this.state.routeChain.push({ |
| 53 | + path: route.path |
| 54 | + }); |
| 55 | + sessionStorage.setItem( |
| 56 | + "$$routeChain", |
| 57 | + JSON.stringify(this.state.routeChain) |
| 58 | + ); |
| 59 | + this.state.addCount++; |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + popRouteChain() { |
| 64 | + this.state.routeChain.pop(); |
| 65 | + sessionStorage.setItem( |
| 66 | + "$$routeChain", |
| 67 | + JSON.stringify(this.state.routeChain) |
| 68 | + ); |
| 69 | + }, |
| 70 | + setPageDirection({ dir, to, from }) { |
| 71 | + this.state.pageDirection = dir; |
| 72 | + this.state.routerMap["to"] = to.path; |
| 73 | + this.state.routerMap["from"] = from.path; |
| 74 | + }, |
| 75 | + setRouterMap() { |
| 76 | + let dir = this.state.pageDirection; |
| 77 | + let to = this.state.routerMap.to.replace(/\//g, "_"); |
| 78 | + let from = this.state.routerMap.from.replace(/\//g, "_"); |
| 79 | + console.log("setRouterMap", dir); |
| 80 | + console.log("setRouterMap", to); |
| 81 | + console.log("setRouterMap", from); |
| 82 | + try { |
| 83 | + if (dir === "slide-left") { |
| 84 | + // 进入 |
| 85 | + this.state.routerMap[from] = document.getElementById(from).scrollTop; |
| 86 | + } else if (dir === "slide-right") { |
| 87 | + // 返回 |
| 88 | + if (this.keepAlive === true && this.$route.meta.keepAlive !== false) { |
| 89 | + document.getElementById(to).scrollTop = this.state.routerMap[to]; |
| 90 | + } |
| 91 | + } else { |
| 92 | + } |
| 93 | + } catch (error) {} |
| 94 | + } |
| 95 | + }, |
| 96 | + mounted() { |
| 97 | + this.$router.beforeEach((to, from, next) => { |
| 98 | + console.log("🐴beforeEach"); |
| 99 | + console.log(to); |
| 100 | + console.log(from); |
| 101 | + |
| 102 | + // 定义一个可以记录路由路径变化的数据,这里用sessionStorage,或者在window.routeChain等变量 |
| 103 | + let routeLength = this.state.routeChain.length; |
| 104 | + if (routeLength === 0 || this.state.addCount === 0) { |
| 105 | + this.setPageDirection({ dir: "slide-left", to, from }); |
| 106 | + this.addRouteChain(from); |
| 107 | + this.addRouteChain(to); |
| 108 | + } else if (routeLength === 1) { |
| 109 | + this.setPageDirection({ dir: "slide-left", to, from }); |
| 110 | + this.addRouteChain(to); |
| 111 | + } else { |
| 112 | + let lastBeforeRoute = this.state.routeChain[routeLength - 2]; |
| 113 | + if (lastBeforeRoute.path === to.path && to.meta.slideLeft !== true) { |
| 114 | + this.popRouteChain(); |
| 115 | + this.setPageDirection({ dir: "slide-right", to, from }); |
| 116 | + } else { |
| 117 | + this.addRouteChain(to); |
| 118 | + this.setPageDirection({ dir: "slide-left", to, from }); |
| 119 | + } |
| 120 | + } |
| 121 | + next(); |
| 122 | + }); |
| 123 | + }, |
| 124 | + activated() { |
| 125 | + console.log("😁😁😁 " + this.state); |
| 126 | + } |
| 127 | +}; |
| 128 | +</script> |
| 129 | + |
| 130 | +<style lang="less"> |
| 131 | +.vue-route-transition { |
| 132 | + position: absolute; |
| 133 | + width: 100%; |
| 134 | + height: 100%; |
| 135 | + display: flex; |
| 136 | + flex-direction: column; |
| 137 | + overflow: hidden; |
| 138 | + backface-visibility: hidden; |
| 139 | + perspective: 1000; |
| 140 | +} |
| 141 | +.fade-enter-active { |
| 142 | + animation: pageFadeIn 400ms forwards; |
| 143 | +} |
| 144 | +@keyframes pageFadeIn { |
| 145 | + from { |
| 146 | + opacity: 0; |
| 147 | + } |
| 148 | + to { |
| 149 | + opacity: 1; |
| 150 | + } |
| 151 | +} |
| 152 | +/*路由前进,退出*/ |
| 153 | + |
| 154 | +.slide-left-leave-active { |
| 155 | + animation: pageFromCenterToLeft 400ms forwards; |
| 156 | + z-index: 1; |
| 157 | +} |
| 158 | + |
| 159 | +/*路由后退,进入*/ |
| 160 | + |
| 161 | +.slide-right-enter-active { |
| 162 | + animation: pageFromLeftToCenter 400ms forwards; |
| 163 | + z-index: 1; |
| 164 | +} |
| 165 | + |
| 166 | +/*路由后退,退出*/ |
| 167 | + |
| 168 | +.slide-right-leave-active { |
| 169 | + animation: pageFromCenterToRight 400ms forwards; |
| 170 | + z-index: 10; |
| 171 | + box-shadow: -3px 0 5px rgba(0, 0, 0, 0.1); |
| 172 | +} |
| 173 | + |
| 174 | +/*路由前进,进入*/ |
| 175 | + |
| 176 | +.slide-left-enter-active { |
| 177 | + animation: pageFromRightToCenter 400ms forwards; |
| 178 | + z-index: 10; |
| 179 | + box-shadow: -3px 0 5px rgba(0, 0, 0, 0.1); |
| 180 | +} |
| 181 | + |
| 182 | +/*-------------------------------*/ |
| 183 | + |
| 184 | +/*路由前进,进入*/ |
| 185 | + |
| 186 | +@keyframes pageFromRightToCenter { |
| 187 | + from { |
| 188 | + /* left: 100%; */ |
| 189 | + transform: translate3d(100%, 0, 0); |
| 190 | + opacity: 1; |
| 191 | + } |
| 192 | + to { |
| 193 | + /* left: 0; */ |
| 194 | + transform: translate3d(0, 0, 0); |
| 195 | + opacity: 1; |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +/*路由前进,退出*/ |
| 200 | + |
| 201 | +@keyframes pageFromCenterToLeft { |
| 202 | + from { |
| 203 | + opacity: 1; |
| 204 | + transform: translate3d(0, 0, 0); |
| 205 | + /* left:0; */ |
| 206 | + } |
| 207 | + to { |
| 208 | + opacity: 0.5; |
| 209 | + transform: translate3d(-20%, 0, 0); |
| 210 | + /* left:-20%; */ |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/*路由后退,进入*/ |
| 215 | + |
| 216 | +@keyframes pageFromLeftToCenter { |
| 217 | + from { |
| 218 | + opacity: 0.5; |
| 219 | + transform: translate3d(-20%, 0, 0); |
| 220 | + /* left: -20%; */ |
| 221 | + } |
| 222 | + to { |
| 223 | + opacity: 1; |
| 224 | + transform: translate3d(0, 0, 0); |
| 225 | + /* left: 0; */ |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +/*路由后退,退出*/ |
| 230 | + |
| 231 | +@keyframes pageFromCenterToRight { |
| 232 | + from { |
| 233 | + /* left: 0; */ |
| 234 | + transform: translate3d(0, 0, 0); |
| 235 | + opacity: 1; |
| 236 | + } |
| 237 | + to { |
| 238 | + /* left: 100%; */ |
| 239 | + transform: translate3d(100%, 0, 0); |
| 240 | + opacity: 1; |
| 241 | + } |
| 242 | +} |
| 243 | +</style> |
0 commit comments