24 Vue小案例--TodoList

通过Vue实现TodoList

代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ToDoList</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        html {
            background-color: #f3eeee;
        }

        .container {
            margin-top: 100px;
        }

        /*输入框*/
        .text-input {
            width: 400px;
            height: 40px;
            background-color: white;
            font-size: 20px;

        }

        /*去掉圆点*/
        li {
            list-style: none;
        }

        /*美化一下*/
        .todos {
            width: 400px;
            height: 40px;
            border: 1px solid #999;
            background-color: white;
            font-size: 20px;
            padding: 1px;
            text-align: left;
        }

        /*浮动动画*/
        .todos:hover {
            background-color: #d9b1b1;
            transition: all 0.8s ease;
        }

        /*添加动画*/
        .v-enter,
        .v-leave-to {
            opacity: 0;
            transform: translateY(60px);
        }

        .v-enter-active,
        .v-leave-active {
            transition: all 0.6s ease;
        }

        /*v-move指的是元素移动时自动添加的类名*/
        .v-move {
            transition: all 0.6s ease;
        }

        .v-leave-active {
            position: absolute;
        }

        .title {
            font-weight: lighter;
            font-size: 60px;
            line-height: 48px;
            color: #c5c1c1;
        }

        /*叉号*/
        .incorrect:before {
            content: '\2716';
            color: #a19798;
            float: right;
            margin-top: 2%;
        }
    </style>
</head>

<body>
    <div class="container">
        <center>
            <div id="app">
                <ul>
                    <li>
                        <!--标题-->
                        <h1 class="title">todos</h1>
                    </li>
                    <!--v-for循环展示每一个todo-->
                    <li> <input type="text" class="text-input" v-model="todo" @keyup.enter="add" /><i
                            class="fa fa-close"></i> </li>
                    <transition-group>
                        <li v-for="(item,i) in todolist" :key="item.id" class="todos">{{item.todo}}
                            <span class="incorrect" @click="del(i)"></span>
                        </li>
                    </transition-group>
                </ul>
            </div>
        </center>
    </div>
    <script>
        var vm = new Vue({
            el: ".container",
            data: {
                todo: "",
                todolist: []
            },
            methods: {
                // 添加方法
                add() {
                    this.todolist.push({
                        id: this.todolist.length,
                        todo: this.todo
                    });
                    this.todo = "";
                },
                // 删除方法
                del(i) {
                    this.todolist.splice(i, 1)
                }
            }
        })
    </script>
</body>

</html>

效果:

在这里插入图片描述

设置了添加动画、删除动画、鼠标浮动动画,回车添加,点击叉号删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以参考以下代码实现一个简单的 Vue TodoList: ``` <template> <div class="todo-list"> <h1>Vue TodoList</h1> <form @submit.prevent="addItem"> <input type="text" v-model="newItem" placeholder="Add item..."> <button type="submit">Add</button> </form> <ul> <li v-for="(item, index) in items" :key="index"> <span>{{ item }}</span> <button @click="deleteItem(index)">Delete</button> </li> </ul> </div> </template> <script> export default { data() { return { newItem: '', items: [] } }, methods: { addItem() { if (this.newItem !== '') { this.items.push(this.newItem); this.newItem = ''; } }, deleteItem(index) { this.items.splice(index, 1); } } } </script> <style> .todo-list { margin: 0 auto; max-width: 600px; } ul { list-style: none; margin: 0; padding: 0; } li { display: flex; justify-content: space-between; align-items: center; margin: 10px 0; padding: 10px; border: 1px solid #ccc; border-radius: 5px; } button { background-color: #f44336; color: #fff; border: none; border-radius: 5px; padding: 5px 10px; cursor: pointer; } button:hover { background-color: #d32f2f; } input[type="text"] { padding: 10px; border-radius: 5px; border: 1px solid #ccc; margin-right: 10px; font-size: 16px; } </style> ``` 在这个 TodoList 中,我们使用了 `v-model` 指令绑定 `newItem` 变量,通过 `addItem` 方法向 `items` 数组中添加新的项目,并使用 `v-for` 指令渲染出每个项目。同时,在每个项目中,我们添加了一个 `Delete` 按钮,用于删除对应的项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值