手摸手,帶你用 vue 動畫實現原生 app 切換效果,絲滑般的體驗


先來看效果圖

效果

完整源碼在 github 中 歡迎 star:

https://github.com/imfing/vuexlearn

准備

開始之前您需要有 vue 基礎,以及安裝好 vue-cli

開始

新建 vue 項目:vue init webpack vuexlearn
記住安裝的時候需要選擇 vue-router

進入 vuexlearn 目錄之后安裝 vuex:
這里使用 npm 安裝 npm install vuex --save 您也可以使用其他方式安裝,具體請參考 vuex 官方文檔。

在安裝好 vuex 之后,您就可以使用 npm run dev 命令運行您的 web 應用了。

現在在 main.js 文件中引入 vuex

main.js

import vuex from 'vuex'
Vue.use(vuex);

在下方添加我們的 vuex 狀態樹

var store = new vuex.Store({//store對象
  state: {
    states: 'turn-on'
  },
  mutations: {
    setTransition(state, states) {
      state.states = states
    }
  }
})

state.states 就是用來記錄我們目前的切換狀態, turn-on 為頁面入棧,turn-off 是頁面出棧。

setTransition(state, states) 方法用來設置 states 的值,在需要的時候我們會調用它。

接下來,我們新建一個 common 組件,作為我們的作標題欄

common.vue

<template>
    <div class="header">
        <div class="left" @click="back">
            back
        </div>
        <div class="center">
            {{title}}
        </div>
    </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  props: ["title"],
  methods: {
    back() {
      this.$store.commit("setTransition", "turn-off");
      this.$router.back(-1);
    }
  }
};
</script>

<style scoped>
.header {
  position: fixed;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color: rgb(100, 231, 60);
}
.clearfix {
  overflow: auto;
}
.left {
  position: fixed;
  left: 0px;
  width: 60px;
}
.center {
  left: 50%;
  position: fixed;
}
</style>

這里通過 props 拿到 name 的值,渲染在標題欄上

這里的切換核心就是在點擊返回的時候,設置整個頁面的動畫效果

新建 4 個頁面,其他的頁面雷同,所以這里只貼出一個頁面

A.vue

<template>
    <div class="A">
        <common title="a"></common>
        <div class="bottom">
            bottom
        </div>
    </div>
</template>

<script>
import common from "./common";
export default {
  data() {
    return {};
  },
  components: {
    common
  }
};
</script>

<style scoped>
.A {
  width: 100%;
  height: 100%;
  background-color: rgb(214, 198, 52);
  position: fixed;
}

.bottom {
  width: 100%;
  height: 50px;
  background-color: red;
  position: fixed;
  bottom: 0px;
}
</style>

App.vue

<template>
  <div id="app">
    <router-link to="/A" @click.native="clickLink">A</router-link>
    <router-link to="/B" @click.native="clickLink">B</router-link>
    <router-link to="/C" @click.native="clickLink">C</router-link>
    <router-link to="/D" @click.native="clickLink">D</router-link>
    <transition :name="$store.state.states">
      <router-view/>
    </transition>
    <div>Index Page</div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
    };
  },
  methods: {
    clickLink() {
      this.$store.commit("setTransition", "turn-on");
    }
  },
  mounted() {
    var _this = this;
    window.addEventListener(
      "popstate",
      function(e) {
        _this.$store.commit("setTransition", "turn-off");
      },
      false
    );
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}
.btn {
  width: 50%;
}
html,
body,
#app {
  height: 100%;
}
.turn-on-enter {
  transform: translate3d(100%, 0, 0);
}
.turn-on-leave-to {
  /* transform: translate3d(-20%, 0, 0); */
}
.turn-on-enter-active,
.turn-on-leave-active {
  transition: transform 0.4s ease;
}
.turn-off-enter {
  /* transform: translate3d(-20%, 0, 0); */
}
.turn-off-leave-to {
  transform: translate3d(100%, 0, 0);
}
.turn-off-enter-active,
.turn-off-leave-active {
  transition: transform 0.4s ease;
}
.turn-off-leave-active {
  z-index: 2;
}
</style>

切換效果就在這里定義了,通過 vuex 全局保存變量達到頁面入棧、出棧的動畫效果。

完整源碼在 github 中:

https://github.com/imfing/vuexlearn

最后在看一下效果圖:

效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM