Vue + vue-router forward and backward push and pull animation effects

<template>
    <div id="app">
      <transition :name="slideDirection">
        <router-view class="router-view"/>
      </transition>
    </div>
</template>

<script>
  export default {
    name: 'App',
    data() {
      return {
        slideDirection: 'slide-right', // The default dynamic route changes to slide-right
      }
    },
    watch: {
      /* eslint-disable */
      '$route'(to, from) {
        const toDepth = to.path.split('/').length
        const fromDepth = from.path.split('/').length
        this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'

      },
    },
  }
</script>

<style lang="less">
  @import '~vux/src/styles/reset.less';
</style>
<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #666666;
    font-size: .28rem;
  }

  .router-view {
    position: absolute;
    width: 100%;
    transition: all .3s ease;
  }

  .slide-left-enter,
  .slide-right-leave-active {
    opacity: 0;
    -webkit-transform: translate(100%, 0);
    transform: translate(100%, 0);
  }

  .slide-left-leave-active,
  .slide-right-enter {
    opacity: 0;
    -webkit-transform: translate(-100%, 0);
    transform: translate(-100% 0);
  }
</style>

The above method of judging whether to go forward or backward is not perfect. There is also a way to directly add a goBack method to the vue-router prototype, and then modify the class name of the view in goBack. When I tried it, it was unsuccessful.

VueRouter.prototype.goBack = function () {
  this.isBack = true
  window.history.go(-1)
}
watch: {
   '$route' (to, from) {
    let isBack = this.$router.isBack // Whether the status of listening for route changes is forward or backward
      if(isBack) {
        this.transitionName = 'slide-right'
      } else {
             this.transitionName = 'slide-left'
     }
  this.$router.isBack = false
  }
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324689809&siteId=291194637