使用转换为待办事项列表中的 adding/deleting 项设置动画

Animate adding/deleting items from todo list using transition

我有一个基于官方 vuejs 文档中提供的示例的待办事项列表。

示例:https://jsfiddle.net/87Lfbzng/

<ul class="todo-list">
  <li v-for="todo in filteredTodos" class="todo" :key="todo.id" :class="{ completed: todo.completed, editing: todo == editedTodo }">
    <div class="view">
      <input class="toggle" type="checkbox" v-model="todo.completed">
      <label @dblclick="editTodo(todo)">{{ todo.title }}</label>
      <button class="destroy" @click="removeTodo(todo)"></button>
    </div>
    <input class="edit" type="text" v-model="todo.title" v-todo-focus="todo == editedTodo" @blur="doneEdit(todo)" @keyup.enter="doneEdit(todo)" @keyup.esc="cancelEdit(todo)">
  </li>
</ul>

问题是这些项目 removed/added 是即时的,没有动画。如果我按照官方文档实现 transition 标签就会生效。 VueJS transitions

我试过将过渡标签放在 ul 中,但也没有用。

我目前的尝试:https://jsfiddle.net/87Lfbzng/

CSS

.fade-enter-active,
.fade-leave-active {
  transition: opacity .5s;
}

.fade-enter,
.fade-leave-to
/* .fade-leave-active below version 2.1.8 */

  {
  opacity: 0;
}

标记

<transition name="fade">
  <ul class="todo-list">
    <li v-for="todo in filteredTodos" class="todo" :key="todo.id" :class="{ completed: todo.completed, editing: todo == editedTodo }">
      <div class="view">
        <input class="toggle" type="checkbox" v-model="todo.completed">
        <label @dblclick="editTodo(todo)">{{ todo.title }}</label>
        <button class="destroy" @click="removeTodo(todo)"></button>
      </div>
      <input class="edit" type="text" v-model="todo.title" v-todo-focus="todo == editedTodo" @blur="doneEdit(todo)" @keyup.enter="doneEdit(todo)" @keyup.esc="cancelEdit(todo)">
    </li>

  </ul>
</transition>

你可以用普通的 css

.view {
   animation: fade-anim 0.4s ease
}
@keyframes fade-anim {
0% {
    opacity: 0;
}
100% {
    opacity: 1;
}

}

您需要将 ul 标签替换为 transition 标签。然后,将 tag 属性添加到 transition 标记,值为 ul

<transition-group name="fade" tag="ul" class="todo-list">

https://jsfiddle.net/ducwpeam/

官方文档对此进行了介绍,以ul为例:VueJs Documentation: List Moves

您仅将 ul 包裹在 transition 内...这将无济于事.. 因为您的 ul 是静态的 :

Vue provides a transition wrapper component, allowing you to add entering/leaving transitions for any element or component in the following contexts:

  • Conditional rendering (using v-if)

  • Conditional display (using v-show)

  • Dynamic components

  • Component root nodes

相反,您需要将 li 元素包装在 transition 标签中,这次它将是 transition-group 因为您有两个以上的元素:

<ul>
 <transition-group name="fade">
  <li></li>
  <li></li> 
   ...
 </transition-group>
</ul>

jsfiddle