Vue中的页面幻灯片过渡?

时间:2019-12-15 13:26:57

标签: vue.js

我正在使用Vue.js构建一个应用程序,我想为其提供更多类似本机的视图。如何添加页面幻灯片过渡?

我当前的代码:

<transition name="fade" mode="out-in">
  <router-view></router-view>
</transition>
...
.fade-enter-active, .fade-leave-active {
  transition: opacity .2s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}

我希望它看起来像

Click here


使用@Ifaruki的代码:

Click here

2 个答案:

答案 0 :(得分:2)

在该示例中,您显示了两个过渡:前进,后退。因此,您将必须找到一种方法来更改转换,具体取决于您单击的链接。您可以为此使用vuex存储。

安装并配置好vuex之后,您可以执行以下操作:

在代码和框中检查实时示例:https://codesandbox.io/s/vuejs-transition-slide-in-out-yzc05

SCSS:设置过渡样式(App.vue中为P.ex)


$duration: 0.5s;
.transition {
  overflow: hidden;
}
.router-view,
.router-view-back {
  &-enter-active,
  &-leave-active {
    position: fixed;
    width: 100%;
    background: white;
    min-height: 100vh;
    top: 0;
  }
}
// router view
.router-view-enter-active {
  transition: transform $duration ease-in-out;
  z-index: 2;
  transform: translateX(100%);
}
.router-view-enter-to {
  z-index: 2;
  transform: translateX(0%);
}
.router-view-leave-active {
  z-index: -1;
}
.router-view-leave-to {
  z-index: -1;
}
// router view back

.router-view-back-leave-active {
  transition: transform $duration ease-in-out;
  z-index: 2;
  transform: translateX(0%);
}
.router-view-back-leave-to {
  z-index: 2;
  transform: translateX(100%);
}

App.vue的模板

<template>
  <div id="app">
    <transition
      :name="$store.state.pageTransition.name"
      :mode="$store.state.pageTransition.mode"
      v-on:after-enter="afterEnter"
      v-on:after-leave="afterLeave"
    >
      <router-view class="transition"/>
    </transition>
  </div>
</template>

App.vue的方法

methods: {
    afterEnter: () => {
      window.scrollTo(0, 0);
    },
    afterLeave: () => {
      Store.commit("setPageTransition", "default");
    }
  }

链接上一个(更改过渡)

模板

 <button @click="goBack()">Previous page</button>

方法

goBack() {
      this.$store.commit("setPageTransition", "back");
      this.$router.push({
        name: "home"
      });
}

答案 1 :(得分:0)

在进行页面转换之前,您应该了解Vue.js路由器。

https://router.vuejs.org/guide/advanced/transitions.html#per-route-transition

编辑:幻灯片效果应如下所示:

 transition name="slide" mode="out-in">
   <router-view></router-view>
 </transition>
 ...
 .slide-enter-active, .slide-leave-active {
   transition: 300ms;

  }
 .slide-enter-to{
    position: relative;
    left: 0;
 }
 .slide-leave{
    position: absolute;
   }
 .slide-enter,  {
    left: -100vw;
    position: absolute;
  }
 .slide-leave-to {
    right: -100vw;
 }