1. 程式人生 > >vue路由組件左右切換(×××返回鍵或自寫按鈕)

vue路由組件左右切換(×××返回鍵或自寫按鈕)

vue 路由 路由切換動畫

1 安裝vue-cli

(1) 下載安裝node.js

(1) npm install -g cnpm --registry=https://registry.npm.taobao.org 換源

(1) cnpm install -g vue-cli 全局安裝vue-cli 已安裝則不用

(1) vue init webpack Vue-Project 初始化vue項目 使用webpack模板,項目名稱(自帶webpack)

(1) cnpm install 初始化 生成node_modules

(1) npm run dev
需要上線打包為 npm run build

2 配置路由 (router 目錄>index.js)

import ccc from ‘@/components/ccc‘

Router.prototype.go = function (a) {
   sessionStorage.isBack = true
  if(a){
    window.history.go(a)
  }else{
    window.history.go(-1)
  }
}
window.addEventListener("popstate", function(e) {
  sessionStorage.isBack = true
}, false);

Vue.use(Router)
3 app.vue中
(1)```
<transition :name="transitionName" >
<router-view class="child-view" ></router-view>
</transition>
</div>


(2)<script>
export default {
  name: ‘App‘,
  data () {
    return {
      transitionName:‘slide-left‘
    }
  },
  watch: {
    ‘$route‘ (to, from) {

      let isBack = eval(sessionStorage.isBack)
      if (isBack) {
        this.transitionName = ‘slide-right‘
      } else {
        this.transitionName = ‘  slide-left‘
      }

      console.log(sessionStorage.isBack);
      sessionStorage.isBack = false
    }
  },

}
</script>

<style>
  *{
    margin: 0 ;
    padding: 0;
  }
  body,html{
    width: 100%;
    height: 100%;
  }
#app {
  font-family: ‘Avenir‘, Helvetica, Arial, sans-serif;
  width: 100%;
  height: 100%;
  text-align: center;
  max-width: 750px;
  margin: 0 auto;
  overflow-x: hidden;
  position: relative;
}
  .child-view {
    transition:all .3s ease;
    position: absolute;
    width: 100%;

  }
  .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>

vue路由組件左右切換(×××返回鍵或自寫按鈕)