Vue过渡群-如何防止“跳跃”?

hwazgwia  于 2023-02-24  发布在  Vue.js
关注(0)|答案(2)|浏览(193)

我有两个div,我使用transition-group在它们之间转换,这是应该的-然而,div转换下面的内容,是“跳跃”取决于div的高度。我想要的是跳跃被阻止,而是它以某种方式动画化,所以当在元素之间切换时,我得到了一个很好的平滑过渡,而不是它“推”到内容与“跳跃”。
希望有道理:)
我在这里设置了一个关于codesandbox的示例: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>
aoyhnmkz

aoyhnmkz1#

尝试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 class="ele" :style="eleStyle">Prevent this div from jumping<br /></div>
bfnvny8b

bfnvny8b2#

您可以尝试:

  • Vue有一个用于组转换的*-move类。但是,transition-group必须应用于所有元素,包括具有v-move类的元素,才能工作。
  • 如果您需要了解更多详细信息,请点击文章链接:https://vuejs.org/guide/built-ins/transition-group.html#move-transitions

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

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

相关问题