简体   繁体   English

为什么从Vue.js中的列表中删除项目时移动转换需要绝对位置

[英]Why is position absolute needed for move transition when removing item from list in Vue.js

https://v2.vuejs.org/v2/guide/transitions.html#List-Move-Transitions gives an example in which when an item is removed from a list, the other items smoothly move in its place. https://v2.vuejs.org/v2/guide/transitions.html#List-Move-Transitions给出了一个示例,当从列表中删除一个项目时,其他项目平滑地移动到它的位置。 For this to work the element is styled with:为此,该元素的样式为:

.list-complete-leave-active {
  position: absolute;
}

I wonder why it doesn't work without this?我想知道为什么没有这个它不起作用?

 new Vue({ el: '#list-complete-demo', data: { items: [1,2,3,4,5,6,7,8,9] }, methods: { randomIndex: function () { return Math.floor(Math.random() * this.items.length) }, remove: function () { this.items.splice(this.randomIndex(), 1) } } })
 .list-complete-item { transition: all 1s; display: inline-block; margin-right: 10px; } .list-complete-leave-to { opacity: 0; transform: translateY(30px); } .list-complete-leave-active { position: absolute; }
 <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.1/lodash.min.js"></script> <div id="list-complete-demo" class="demo"> <button v-on:click="remove">Remove</button> <transition-group name="list-complete" tag="p"> <span v-for="item in items" v-bind:key="item" class="list-complete-item" > {{ item }} </span> </transition-group> </div>

In this case, the position of the actively removed number is set from static to absolute , so that the element does not take up any space.在这种情况下,主动移除的数字的位置从static设置为absolute ,这样元素就不会占用任何空间。 Why is this important?为什么这很重要? The animation slides the rest of the numbers to the left, and if the removed item takes up space, this does not happen.动画将其余数字向左滑动,如果删除的项目占用空间,则不会发生这种情况。 You could substitute this for, for example, position: fixed or margin-right: -8px .例如,您可以将其替换为position: fixedmargin-right: -8px All of these will animate the bounding box from about 18px (8px from the number, 10 from the margin) to 0px, neatly sliding the rest of the inline positioned items to the left.所有这些都会将边界框从大约 18px(数字 8px,边距 10)设置为 0px,将其余的内联定位项整齐地向左滑动。

 new Vue({ el: '#list-complete-demo', data: { items: [1,2,3,4,5,6,7,8,9] }, methods: { randomIndex: function () { return Math.floor(0) }, remove: function () { this.items.splice(this.randomIndex(), 1) } } })
 .list-complete-item { transition: all 10s; display: inline-block; margin-right: 10px; } .list-complete-leave-to { opacity: 0; transform: translateY(30px); } .list-complete-leave-active { position: absolute; }
 <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.1/lodash.min.js"></script> <div id="list-complete-demo" class="demo"> <button v-on:click="remove">Remove</button> <transition-group name="list-complete" tag="p"> <span v-for="item in items" v-bind:key="item" class="list-complete-item" > {{ item }} </span> </transition-group> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM