App.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div id="app">
  3. <transition :name="transitionName">
  4. <keep-alive>
  5. <router-view class="child-view"></router-view>
  6. </keep-alive>
  7. </transition>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. data () {
  13. return {
  14. transitionName: null,
  15. }
  16. },
  17. mounted() {
  18. this.sessionStorage('/', 1);
  19. //
  20. let hash = window.location.hash;
  21. if (hash.indexOf("#") === 0) {
  22. hash = hash.substr(1);
  23. if (hash !== '/' && this.sessionStorage(hash) === 0) {
  24. this.sessionStorage(hash, this.sessionStorage('::count') + 1);
  25. }
  26. }
  27. },
  28. watch: {
  29. '$route' (To, From) {
  30. if (this.transitionName === null) {
  31. this.transitionName = 'app-slide-no';
  32. return;
  33. }
  34. if (typeof To.name === 'undefined' || typeof From.name === 'undefined') {
  35. return;
  36. }
  37. this.slideType(To, From)
  38. }
  39. },
  40. methods: {
  41. slideType(To, From) {
  42. let isBack = this.$router.isBack;
  43. this.$router.isBack = false;
  44. //
  45. let ToIndex = this.sessionStorage(To.path);
  46. let FromIndex = this.sessionStorage(From.path);
  47. if (ToIndex && ToIndex < FromIndex) {
  48. isBack = true; //后退
  49. this.sessionStorage(true, ToIndex);
  50. }else{
  51. isBack = false; //前进
  52. this.sessionStorage(To.path, this.sessionStorage('::count') + 1);
  53. }
  54. //
  55. if (To.meta.slide === false || From.meta.slide === false)
  56. {
  57. //取消动画
  58. this.transitionName = 'app-slide-no'
  59. }
  60. else if (To.meta.slide === 'up' || From.meta.slide === 'up' || To.meta.slide === 'down' || From.meta.slide === 'down')
  61. {
  62. //上下动画
  63. if (isBack) {
  64. this.transitionName = 'app-slide-down'
  65. } else {
  66. this.transitionName = 'app-slide-up'
  67. }
  68. }
  69. else
  70. {
  71. //左右动画(默认)
  72. if (isBack) {
  73. this.transitionName = 'app-slide-right'
  74. } else {
  75. this.transitionName = 'app-slide-left'
  76. }
  77. }
  78. },
  79. sessionStorage(path, num) {
  80. let conut = 0;
  81. let history = JSON.parse(window.sessionStorage['__history__'] || '{}');
  82. if (path === true) {
  83. let items = {};
  84. for(let i in history){
  85. if (history.hasOwnProperty(i)) {
  86. if (parseInt(history[i]) <= num) {
  87. items[i] = history[i];
  88. conut++;
  89. }
  90. }
  91. }
  92. history = items;
  93. history['::count'] = Math.max(num, conut);
  94. window.sessionStorage['__history__'] = JSON.stringify(history);
  95. return history;
  96. }
  97. if (typeof num === 'undefined') {
  98. return parseInt(history[path] || 0);
  99. }
  100. if (path === "/") num = 1;
  101. history[path] = num;
  102. for(let key in history){ if (history.hasOwnProperty(key) && key !== '::count') { conut++; } }
  103. history['::count'] = Math.max(num, conut);
  104. window.sessionStorage['__history__'] = JSON.stringify(history);
  105. }
  106. }
  107. }
  108. </script>
  109. <style>
  110. body { overflow-x: hidden; }
  111. </style>
  112. <!--suppress CssUnusedSymbol -->
  113. <style scoped>
  114. .child-view {
  115. position: absolute;
  116. width: 100%;
  117. min-height: 100%;
  118. background-color: #f1f2f7;
  119. transition: all .3s cubic-bezier(.55, 0, .1, 1);
  120. }
  121. .app-slide-no-leave-to {display: none;}
  122. /**
  123. * 左右模式
  124. */
  125. .app-slide-left-leave-active{z-index:1;transform:translate(0,0)}
  126. .app-slide-left-leave-to{z-index:1;transform:translate(0,0)}
  127. .app-slide-left-enter-active{opacity:0;z-index:2;transform:translate(30%,0)}
  128. .app-slide-left-enter-to{opacity:1;z-index:2;transform:translate(0,0)}
  129. .app-slide-right-leave-active{opacity:1;z-index:2;transform:translate(0,0)}
  130. .app-slide-right-leave-to{opacity:0;z-index:2;transform:translate(30%,0)}
  131. .app-slide-right-enter-active{z-index:1;transform:translate(0,0)}
  132. .app-slide-right-enter{z-index:1;transform:translate(0,0)}
  133. /**
  134. * 上下模式
  135. */
  136. .app-slide-up-leave-active{z-index:1;transform:translate(0,0)}
  137. .app-slide-up-leave-to{z-index:1;transform:translate(0,0)}
  138. .app-slide-up-enter-active{opacity:0;z-index:2;transform:translate(0,20%)}
  139. .app-slide-up-enter-to{opacity:1;z-index:2;transform:translate(0,0)}
  140. .app-slide-down-leave-active{opacity:1;z-index:2;transform:translate(0,0)}
  141. .app-slide-down-leave-to{opacity:0;z-index:2;transform:translate(0,20%)}
  142. .app-slide-down-enter-active{z-index:1;transform:translate(0,0)}
  143. .app-slide-down-enter{z-index:1;transform:translate(0,0)}
  144. </style>