gpt4 book ai didi

javascript - Vue.js 仅在最后一个元素上应用动画

转载 作者:行者123 更新时间:2023-12-05 02:51:08 30 4
gpt4 key购买 nike

我正在练习 Vue transition-group 并专门在 上模仿 Vue 文档示例 THIS 特定部分。

问题是我的示例将仅在结束元素而不是正在添加/删除的元素上应用动画。

有人可以解释一下我做错了什么吗? :( 谢谢。

Here is link to the app for more clarification

<style>
<head>
body{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
margin: 0;
font-family: 'Nunito', sans-serif;
background-color: #101010;
color: white;
text-align: center;
}
.flex{display:flex;justify-content:center;}

.test-enter{
opacity: 0;
transform: translateY(-20px);
}
.test-enter-to{
opacity: 1;
}

.test-leave-to{
opacity: 0;
transform: translateY(20px);
}

.test-enter-active,
.test-leave-active{
transition: 0.5s ease;
}
</style>
</head>
<body>
<br>

<div id='app'>
<button @click='shuffle'>Shuffle</button>
<button @click='add'>Add</button>
<button @click='remove'>Remove</button>
<br><br>

<transition-group name='test' class='flex'>
<div v-for='(num, ind) in arr' :key='ind' style='border: 1px solid red;'>{{ num }}</div>
</transition-group>
</div>

<script>

let theArray = [1,2,3,4,5,6,7,8];

new Vue({
el: '#app',
data: {
arr: theArray
},
computed: {
arrLength(){
return this.arr.length;
}
},
methods: {
shuffle(){
alert('Not working yet');
},
add(){
// Number from 1 - 9.
let ranNum = Math.floor(Math.random()*9+1);
// Get a random index of current array length.
let ranInd = Math.floor(Math.random()*this.arrLength);

this.arr.splice(ranInd, 0, ranNum);
},
remove(){
let ranInd = Math.floor(Math.random()*this.arrLength);

this.arr.splice(ranInd, 1);
}
}
});

</script>

最佳答案

只需将键更改为 num 而不是 ind :

 <div v-for='(num, ind) in arr' :key='num' style='border: 1px solid red;'>{{ num }}</div>

因为这允许转换要动画的数字而不是索引。

关于javascript - Vue.js <transition-group> 仅在最后一个元素上应用动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63308808/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com