1

我有两个 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>
4

2 回答 2

1

试试这个

  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 class="ele" :style="eleStyle">Prevent this div from jumping<br /></div>
于 2021-04-29T06:13:52.833 回答
0

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

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

    • 如果您需要更多详细信息,请查看文章链接:https ://v3.vuejs.org/guide/transitions-list.html#list-entering-leaving-transitions 。

      v-move基本上将项目从其原始位置动画到新位置,使其平滑,而不是跳跃)

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

于 2021-05-26T20:25:27.907 回答