Vue:out-in,使in元素在out元素消失之前出现

14ifxucb  于 12个月前  发布在  Vue.js
关注(0)|答案(1)|浏览(145)

Codepen:https://codesandbox.io/s/priceless-architecture-vp4jwn?file=/src/App.vue
我有两个div元素:Block 1Block 2
当一个按钮被点击时,Block 1会向左平移,直到它从视图中消失。然后Block 2会从父div的右侧出现。
然而,Block 2直到Block 1完全从视野中消失才开始移动。
有没有办法让Block 2translateX属性的50%上出现?

<template>
  <div id="app">
    <button @click="show = !show">Click Me!</button>
    <transition mode="out-in">
      <div class="block" id="block-1" v-if="show">Block 1</div>
      <p class="block" id="block-2" v-else>Block 2</p>
    </transition>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return { show: true };
  },
};
</script>

<style>
body {
  font-family: 微軟正黑體;
}
button {
  margin-bottom: 10px;
}

#block-1 {
  background-color: yellow;
  color: black;
}

#block-2 {
  background-color: green;
}

#app {
  padding: 10px;

  background-color: gray;
  overflow: hidden;
}

.block {
  background: #999;
  color: #fff;
  display: table-cell;
  width: 100px;
  height: 100px;
  text-align: center;
  vertical-align: middle;
}

.v-leave-from {
  transform: translateX(0%);
}
.v-leave-active {
  transition: transform 1s;
}
.v-leave-to {
  transform: translate(-220%);
}
.v-enter-from {
  transform: translateX(600%);
}
.v-enter-active {
  transition: transform 2s;
}
.v-enter-to {
  transform: translateX(0%);
}
</style>

字符串

hgc7kmma

hgc7kmma1#

您是否尝试了无模式(默认模式),然后新旧元素同时转换:

const app = Vue.createApp({
  data() {
    return { show: true };
  },
})
app.mount('#demo')
body {
  font-family: 微軟正黑體;
}
button {
  margin-bottom: 10px;
}

#block-1 {
  background-color: yellow;
  color: black;
}

#block-2 {
  background-color: green;
}

#app {
  padding: 10px;

  background-color: gray;
  overflow: hidden;
}

.block {
  background: #999;
  color: #fff;
  display: table-cell;
  width: 100px;
  height: 100px;
  text-align: center;
  vertical-align: middle;
}

.v-leave-from {
  transform: translateX(0%);
}
.v-leave-active {
  transition: transform 1s;
}
.v-leave-to {
  transform: translate(-220%);
}
.v-enter-from {
  transform: translateX(600%);
}
.v-enter-active {
  transition: transform 2s;
}
.v-enter-to {
  transform: translateX(0%);
}
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
    <button @click="show = !show">Click Me!</button>
    <transition >
      <div class="block" id="block-1" v-if="show">Block 1</div>
      <p class="block" id="block-2" v-else>Block 2</p>
    </transition>
  </div>

相关问题