Vue2.0 過渡動畫——仿微信左右切換效果

需要用到的技術(shù)棧Vue+Vuex

先丟個(gè)圖


過渡動畫.gif

awesomes上尋找移動端框架的時(shí)候意外發(fā)現(xiàn)了vux的頁面切換效果,后面由于其他考慮沒有選用但是這個(gè)切換效果確實(shí)感覺很有新意,也就看了下源碼,這里貼一份備用。

//app.vue
<transition :name="'vux-pop-' + (direction === 'forward' ? 'in' : 'out')">
      <keep-alive>
      <router-view class="router-view" ></router-view>
      </keep-alive>
</transition>
<script>
  import { mapState } from 'vuex'
  import sideFooter from "./components/Footer.vue"

  export default {
    name: 'app',
    data () {
      return {
        showFooter : false
      }
    },
    components : {
      sideFooter
    },
    computed:{
      ...mapState({
        direction: state => state.mutations.direction
      })
    },
  }
</script>

<style scoped>
 .vux-pop-out-enter-active,
  .vux-pop-out-leave-active,
  .vux-pop-in-enter-active,
  .vux-pop-in-leave-active {
    will-change: transform;
    transition: all 250ms;
    height: 100%;
    top: 0;
    position: absolute;
    backface-visibility: hidden;
    perspective: 1000;
  }

  .vux-pop-out-enter {
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
  }

  .vux-pop-out-leave-active {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
  }

  .vux-pop-in-enter {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
  }

  .vux-pop-in-leave-active {
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
  }
</style>
// main.js
const history = window.sessionStorage;
history.clear()
let historyCount = history.getItem('count') * 1 || 0;
history.setItem('/', 0);

router.beforeEach(function (to, from, next) {

  const toIndex = history.getItem(to.path);
  const fromIndex = history.getItem(from.path);

  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('UPDATE_DIRECTION', {direction: 'forward'})
    } else {
      store.commit('UPDATE_DIRECTION', {direction: 'reverse'})
    }
  } else {
    ++historyCount;
    history.setItem('count', historyCount);
    to.path !== '/' && history.setItem(to.path, historyCount);
    store.commit('UPDATE_DIRECTION', {direction: 'forward'})
  }
    next()
});

這里還用到了vuex,但是我stroe寫了很多就不提出來了,主要就是通過 UPDATE_DIRECTION方法更新每一次的路由方向是前進(jìn)還是后退。


man.js里面主要思想就是給路由增加一個(gè)索引存到sessionStorage里面,以點(diǎn)擊過的索引值不變,新增加的路由,索引增加1,同時(shí)count+1,這樣在頁面切換時(shí)通過比較索引值的大小,大的向右小的向左,做到左右有規(guī)律的過渡。

好了至此一個(gè)左右切換的過渡效果就成了,最近由于一直在開發(fā)也沒怎么更新文章,如果有朋友有好的想法歡迎與我交流。


另附github地址:https://github.com/Yunfly/vue-wechatAnimation

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容