【问题标题】:Vue transition-group - how to prevent "jumping"?Vue 过渡组 - 如何防止“跳跃”?
【发布时间】:2021-04-28 08:58:17
【问题描述】:

我有两个 div 在使用 transition-group 之间进行转换,它应该可以正常工作 - 但是,div 转换下方的内容是“跳跃”的,具体取决于 div 的高度。 我希望它阻止跳跃,而是以某种方式动画,所以在元素之间切换时我得到了一个很好的平滑过渡,而没有它“推”到“跳跃”的内容..

希望它有意义:)

我在这里设置了一个关于代码框的示例:https://codesandbox.io/s/reverent-stallman-8ixhp?file=/src/components/HelloWorld.vue

模板如下:

<div class="hello">
    <button @click="groupShowOne">Show first {{ gShowFirst }}</button>
    <button @click="groupShowTwo">Show second {{ gShowSecond }}</button>
    <transition-group name="fade-group" tag="div" mode="out-in" appear>
      <div
        class="group-element"
        v-if="gShowFirst"
        style="background-color: yellow"
      >
        <h3>This is a headline</h3>
        <p>This is a text</p>
      </div>
      <div
        class="group-element"
        v-if="gShowSecond"
        style="background-color: red"
      >
        <h3>
          This is a headline <br />This is a headline <br />This is a headline
          This is a headline This is a headline This is a headline
        </h3>
        <p>
          This is a text This is a text This is a text This is a text This is a
          text v This is a text v <br />This is a text This is a text This is a
          text This is a text This is a text v This is a text v <br />This is a
          text This is a text This is a text This is a text This is a text v
          This is a text v
        </p>
      </div>
    </transition-group>
    <div style="background-color: blue; min-height: 500px; color: #FFF">
      Prevent this div from jumping<br />
    </div>
  </div>

动画的样子:

<style scoped>
.group-element {
  width: 100%;
  min-height: 100px;
  max-height: 20000px;
  transition: all 0.5s;
}
.fade-group-enter,
.fade-group-leave-to {
  opacity: 1;
}
.fade-group-leave-active {
  opacity: 0;
  position: absolute;
}
</style>

【问题讨论】:

    标签: vue.js vuejs2 vuejs3 vue-transitions


    【解决方案1】:

    试试this

    1. 在被动 div 中设置过渡属性:
    .ele {
      background-color: blue;
      min-height: 500px;
      color: #fff;
      -moz-transition: all 0.5s;
      -ms-transition: all 0.5s;
      -o-transition: all 0.5s;
      -webkit-transition: all 0.5s;
      transition: all 0.5s;
    }
    
    1. 让它做一些动画
    eleStyle() {
      return {
        transform: this.gShowSecond ? "translate3d(0, 100px, 0)" : "none",
      };
    },
    

    div:

    <div class="ele" :style="eleStyle">Prevent this div from jumping<br /></div>
    

    【讨论】:

    • 但这使得它依赖于设置一个固定的 translateY 大小,在我的情况下它可以是非常动态的....我并不总是知道这个值,所以很遗憾不能这样做.. :/跨度>
    • 另外,下面的 div(防止它跳 div),我并不总是知道这是或可以是哪个元素,这使得它很难处理......实际上可以是任何东西,这要求我需要为每个可能的已知/未知元素添加一个过渡,这并不理想:/
    • 所以我想,在我的情况下,需要有过渡高度的包装 div(淡入淡出组),但我无法让它工作:(
    【解决方案2】:

    我想我明白你在说什么?你可以尝试什么:

    1. Vue 有一个用于组转换的 v-move 类。但是,transition-group 必须应用于所有元素,包括具有 v-move 类的元素才能工作。

    2. showSecondshowFirst 等于某个值时,您仍然可以使用现有的内容并为蓝色框动态绑定单独的 CSS 过渡。

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      相关资源
      最近更新 更多