Vuejs 过渡

Vuejs Transition

我想在值发生变化但无法完成时添加转换。我需要在值更新时更改背景。

谁能帮帮我?

Codesandbox Link - Vuejs Transition

<template>
  <div id="app">
    <div class="box">
      <div v-if="change > 0">
        <transition name="increase">
          <span>+ {{ amount }} </span></transition
        >
      </div>
      <div v-else-if="change < 0">
        <transition name="decrease">
          <span>- {{ amount }}</span>
        </transition>
      </div>
      <div v-else>{{ amount }}</div>
    </div>
    <br />
    <button @click="generate">Update</button>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      change: 2,
      amount: 100,
    };
  },
  methods: {
    generate() {
      var amounts = [100, 110, 85, 75, 190];
      let changes = [-1.2, -5, 2, 5, 1, 7];
      this.amount = amounts[Math.floor(Math.random() * amounts.length)];
      this.change = changes[Math.floor(Math.random() * changes.length)];
    },
  },
  created() {},
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.box {
  width: 100px;
  margin: 0 auto;
  border: 1px solid #ddd;
  padding: 1em 2em;
}
@keyframes higher {
  50% {
    background: rgba(50, 199, 135, 0.2);
    color: #32c787;
  }
}
@keyframes lower {
  50% {
    background: rgba(255, 86, 82, 0.2);
    color: #ff5652;
  }
}
.increase-enter {
  animation: higher 0.5s;
}
.decrease-enter {
  animation: lower 0.5s;
}
</style>

P.S - 忽略这个,发布是因为我的文字太短了。 reader 在查看页面布局时会被页面的可读内容分散注意力,这是一个长期存在的事实。使用 Lorem Ipsum 的要点在于它具有或多或少的正态分布的字母,而不是使用 'Content here, content here',使其看起来像可读的英文。

你可以利用key通知transition有元素的不同,这样它可以再次触发动画。

此外,使用 -active class 在进入阶段应用动画。

https://codesandbox.io/s/transition-forked-vg5pu?file=/src/App.vue

<transition name="increase">
    <span :key="amount + change">+ {{ amount }}</span>
</transition>

...

.increase-enter-active {
  animation: higher 0.5s;
}