【问题标题】:How to get VueJS transitioning Divs beside eachother?如何让 Vue JS 过渡 Div 并排放置?
【发布时间】:2018-10-09 21:43:11
【问题描述】:

当使用带有左右滑动动画的 Vue 过渡时,我怎样才能让 Div 彼此并排?

看看这支笔; https://codepen.io/anon/pen/jeBBaB

HTML

<div class="heading">
  <h1>Transition demo</h1>
  <h4>Why this no work?</h4>
</div>

<div class="container" id="app">

    <transition :enter-active-class="enterAnimation" :leave-active-class="leaveAnimation" mode="">

      <div key="one" v-if="currentStep == 1">
        This is Step One
      <button class="btn btn-primary" @click="currentStep = 2; previousStep=1">Next</button>
      </div>

        <div key="two" v-else>
        This is Step Two
      <button class="btn btn-primary" @click="currentStep = 1; previousStep=2">Back</button>
      </div>

  </transition>

</div>

CSS

$purple: #5c4084;

body {
  background-color: $purple;
  padding: 50px;
}
.container {
  padding: 40px 80px 15px 80px;
  background-color: #fff;
  border-radius: 8px;
  max-width: 800px;
}
.heading {
  text-align: center;
  h1 {
    background: -webkit-linear-gradient(#fff, #999);
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
    text-align: center;
    margin: 0 0 5px 0;
    font-weight: 900;
    font-size: 4rem;
    color: #fff;
  }
  h4 {
    color: lighten(#5c3d86,30%);
    text-align: center;
    margin: 0 0 35px 0;
    font-weight: 400;
    font-size: 24px;
  }
}
.btn{
  outline: none !important;
}
.btn.btn-primary {
  background-color: $purple;
  border-color: $purple;
  outline: none;
  &:hover {
    background-color: darken($purple, 10%);
    border-color: darken($purple, 10%);
  }
  &:active, &:focus {
    background-color: lighten($purple, 5%);
    border-color: lighten($purple, 5%);
  }
  & .fa {
    padding-right: 4px;
  }
}
.form-group {
  margin-bottom: 25px;
}

JS

new Vue({
  el: '#app',
  data: {
    currentStep: 1,
    previousStep: 0
  },
  computed:{
      enterAnimation() {
        if (this.currentStep < this.previousStep) {
          return "animated slower fadeInLeft";
        } else {
          return "animated slower fadeInRight";
        }
      },
      leaveAnimation() {
        if (this.currentStep > this.previousStep) {
          return "animated slower fadeOutLeft";
        } else {
          return "animated slower fadeOutRight";
        }
      }
  }
});

使用no模式时,“进入”的div出现在“离开”的div下面一行,直到结束,然后向上弹出。

我可以使用 mode="out-in" 但 Div 之间存在明显的差距。我只想让一个滑入,紧挨着滑出的那个。有什么方法可以实现吗?

【问题讨论】:

    标签: vue.js animate.css


    【解决方案1】:

    您可以在 div 元素上使用绝对定位,不过您需要稍微调整一下 CSS。

    但作为起点,将您的 .container 规则更改为此(添加位置:相对;):

    .container {
    padding: 40px 80px 15px 80px;
    background-color: #fff;
    border-radius: 8px;
    max-width: 800px;
    position:relative;
    }
    

    并将其添加为新规则:

    .container div {position:absolute;top:0;left:0;}
    

    flexbox方式:

    将您的容器规则更改为:

    .container {
    padding: 40px 80px 15px 80px;
    background-color: #fff;
    border-radius: 8px;
    max-width: 800px;
    display:flex;
    }
    

    在此之后,您可以使用 css 翻译规则来定位内容。您可以在此处查看一个工作示例:

    Vue transitions

    【讨论】:

    • 谢谢,帮助,虽然问题是如果 Step2 的内容很“长”,它会挂在容器的底部:(codepen.io/anon/pen/KGWGbr
    • 好吧,另一种选择是用 flexbox 来做,我会用一些东西来更新我的答案,让你开始
    猜你喜欢
    • 2011-02-07
    • 2011-08-13
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2023-03-06
    • 2013-06-05
    相关资源
    最近更新 更多