如何在Vue.js中滑动转换两个组件

时间:2019-07-03 16:05:48

标签: javascript css vue.js css-transitions

我正在尝试创建一个Vue.js过渡,其中一个组件滑入而另一个组件滑出。目前,我可以按照自己的方式滑动新的组件,但是一直无法使旧的组件滑出。

我想要的过渡效果与TakeCareOf中的过渡效果类似。

    <transition name="slide-fade">
      <reservation-step step="1" :showBack="false">
        <space-selection/>
      </reservation-step>
    </transition>

    <transition name="slide-fade">
      <reservation-step step="2">
        <reservation-type/>
      </reservation-step>
    </transition>

    <transition name="slide-fade">
      <reservation-step step="3">
        <date-selection/>
      </reservation-step>
    </transition>



<style lang='scss'>
.slide-fade-enter-active,
.slide-fade-leave-active {
  transition: all 1s 0.2s;
}
.slide-fade-enter, .slide-fade-leave-active
/* .slide-fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.slide-fade-enter {
  transform: translateX(100px);
}
.slide-fade-leave-active {
  transform: translateX(-100px);
}
</style>

1 个答案:

答案 0 :(得分:0)

这比vue.js更像是一个CSS动画问题,但我希望这个例子对您有所帮助。 On JSFiddle。有关更多信息,请查看官方vue translations documentation

模板

<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<div id="transition-components-demo">

<p>A <input name="component" type="radio" value="v-a" v-model="view"></p>
<p>B <input name="component" type="radio" value="v-b" v-model="view"></p>

<transition 
    name="slide"
>
  <component v-bind:is="view"></component>
</transition>

</div>

vue代码

new Vue({
  el: '#transition-components-demo',
  data: {
    view: 'v-a',
  },
  components: {
    'v-a': {
      template: '<div>Компонент А</div>'
    },
    'v-b': {
      template: '<div>Компонент Б</div>'
    }
  }
})

css动画

.slide-leave-active,
.slide-enter-active {
  transition: 1s;
}
.slide-enter {
  transform: translate(100%, 0);
}
.slide-leave-to {
  transform: translate(-100%, 0);
}