Home > Web Front-end > JS Tutorial > body text

Vue-router combined with transition to realize app animation switching effect example sharing

小云云
Release: 2018-01-25 10:58:18
Original
3155 people have browsed it

This article mainly brings you an example of Vue-router combined with transition to realize the forward and backward animation switching effect of the app. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

1. First configure the routing and modify the routing configuration

The routing configuration will not focus on the point. Add a goBack method to VueRoute to record the forward and backward status of the route

this.isBack = true VueRouter.prototype.goBack = function () { 
  this.isBack = true
  window.history.go(-1)
}
Copy after login

2. Monitor route changes (when the route changes, determine whether the routing status is forward or backward)

<template>
  <p>

    动态绑定路由动画,根据路由状态的不同绑定不同的路由动画分别为 :‘slide-left' 和 'slide-right'

    <transition :name="transitionName"> 
      <router-view class="Router"></router-view>
    </transition>
  </p>
</template>

<script>
export default {
  data() {
    return {
      transitionName: 'slide-right' // 默认动态路由变化为slide-right
    }
  },
  watch: {
   '$route' (to, from) {
    let isBack = this.$router.isBack // 监听路由变化时的状态为前进还是后退
      if(isBack) {
        this.transitionName = 'slide-right'
      } else {
       this.transitionName = 'slide-left'
     }
  this.$router.isBack = false
  }
  }
 }
</script>
Copy after login

3. Add different animation effects to the forward and backward animations. The specific code is as follows :

<style>

.Router {
 position: absolute;
 width: 100%;
 transition: all .8s ease;
 top: 40px;
}

.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>
Copy after login

4. When routing forward, just follow the normal method;

5. When routing backward, just call the goBack method;

Related recommendations:

Comprehensive comparison of CSS3 animation-related properties transition, animation, and transform

Instance analysis of transition properties in CSS3

10 recommended articles about transition

The above is the detailed content of Vue-router combined with transition to realize app animation switching effect example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template