index.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <script>
  2. import './index.css'
  3. export default {
  4. // #ifdef H5
  5. data() {
  6. return {
  7. before: '',
  8. afterstart: '',
  9. isBack: false,
  10. fromId: '0',
  11. startType: true
  12. };
  13. },
  14. onLaunch: function() {
  15. // 监听浏览器返回按钮
  16. window.addEventListener("popstate", (e) => {
  17. this.isBack = true;
  18. }, false);
  19. this.show()
  20. this.$router.beforeEach((toPage, fromPage, next) => {
  21. let type = this.startType
  22. this.before = type ? 'animation-before' : 'animation-before2';
  23. this.afterstart = type ? 'animation-afterstart' : 'animation-afterstart2';
  24. setTimeout(this.hide(next));
  25. })
  26. this.$router.afterEach(() => {
  27. this.isBack = false
  28. setTimeout(this.show, 50)
  29. });
  30. },
  31. watch: {
  32. $route(route) {
  33. if (this.fromId >= route.params.__id__) {
  34. this.startType = true
  35. } else {
  36. this.startType = false
  37. }
  38. this.fromId = route.params.__id__
  39. }
  40. },
  41. methods: {
  42. hide(callback) {
  43. setTimeout(() => {
  44. this.before = this.isBack ? 'animation-before2' : this.before;
  45. this.afterstart = this.isBack ? 'animation-afterstart2' : this.afterstart;
  46. const classList = document.querySelector('uni-page').classList
  47. classList.add(this.before, 'animation-leave')
  48. classList.remove('animation-show')
  49. setTimeout(() => {
  50. classList.remove(this.before, 'animation-leave')
  51. callback && callback();
  52. }, 300);
  53. }, 20)
  54. },
  55. show() {
  56. const classList = document.querySelector('uni-page').classList
  57. classList.add(this.afterstart, 'animation-before')
  58. setTimeout(() => {
  59. classList.add('animation-enter', 'animation-after', 'animation-show')
  60. setTimeout(() => {
  61. classList.remove('animation-before', 'animation-after', 'animation-enter', this.afterstart)
  62. }, 300)
  63. }, 20);
  64. },
  65. },
  66. // #endif
  67. }
  68. </script>