Reputation: 55
I want to add transition when the values get changes but cannot accomplish it. I need to change the background when the values get updated.
Can anyone help me out?
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 - Ignore this, posted because my text is too short. IT is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
Upvotes: 0
Views: 143
Reputation: 5692
You can make use of key
to inform transition there is a difference of the element so it can trigger the animation again.
Also, use the -active
class to apply animation during entering phase.
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;
}
Upvotes: 1