Blog>
Snippets

Todo List Completion Animation

Animate the completion of a todo list item with a strike-through effect, using Vue's <Transition> to visually indicate an item's change in state.
<template>
  <div id="app">
    <ul>
      <li v-for="(todo, index) in todos" :key="todo.id">
        <transition name="strikethrough">
          <div v-if="todo.completed" class="completed">{{ todo.text }}</div>
        </transition>
        <div v-else>{{ todo.text }}</div>
        <button @click="completeTodo(index)">Complete</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      todos: [
        { id: 1, text: 'Buy groceries', completed: false },
        { id: 2, text: 'Call mom', completed: false }
      ],
    };
  },
  methods: {
    completeTodo(index) {
      this.todos[index].completed = !this.todos[index].completed;
    }
  },
};
</script>

<style>
.strikethrough-enter-active, .strikethrough-leave-active {
  transition: opacity 1s;
}
.strikethrough-enter, .strikethrough-leave-to {
  opacity: 0.1;
  text-decoration: line-through;
}
.completed {
  text-decoration: line-through;
}
</style>
In this Vue.js template, a list of todos is displayed, each with a 'Complete' button. When a button is clicked, the completeTodo method toggles the completed state of that todo, triggering a transition effect. The strikethrough transition gives a fade-out effect alongside the line-through decoration when the todo is marked as completed.