vue简单todolist制作

html页面代码

<!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>Document</title>
    <link rel="stylesheet" href="http://g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
    <link rel="stylesheet" href="./index.css">
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
    <div id="app">
        <div class="page-group">
            <div class="page page-current">
                <!-- header -- start -->
                <header class="bar bar-nav">
                    <a class="icon icon-star pull-left"></a>
                    <a class="icon icon-edit pull-right" @click="add_todo_flag=!add_todo_flag"></a><!-- 控制input框的出现与消失 -->
                    <h1 class="title">todo_list</h1>
                </header>
                <!-- header -- end -->

                <!-- content -- start -->
                <div class="content">
                    <input
                     type="text" 
                     v-show="add_todo_flag"     
                     v-model = "add_todo_input"
                     @focus="getFocus"
                     @keyup.enter="addTodoItem"
                     ><!-- 控制input框的出现消失,双向数据绑定,框内信息,按下回车键push方法向数组添加数据渲染界面 -->
                    <div 
                    class="card" 
                    v-for = "(todo,index) in newTodos"
                    :key = "todo.id"><!-- 根据不同的选项渲染不同的界面 -->
                        <div class="card-content">
                            <div class="card-content-inner">{{ todo.task }}</div>
                            <div class="my-btn-box pull-right">
                                <button
                                 class="button button-success"
                                 @click = "todo.flag = !todo.flag"
                                 :class="[todo.flag?'button-fill':'']"
                                 ><!-- flag控制是否打钩 -->
                                    <i class="icon icon-check"></i>
                                </button>

                                <button class="button button-danger"
                                @click = "check( index )"><!-- 判断是否打钩,打钩直接删除,没打勾弹出选项框 -->
                                    <i class="icon icon-remove"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- content -- end -->

                <!-- footer --start -->
                <footer>
                    <ul>
                        <li class="circle"
                        v-for="btn in btns"
                        :key="btn.id"
                        :class="['circle-'+btn.mold,btn.name===type?'circle-fill':'']"
                        @click="type=btn.name">{{btn.content}}</li><!-- 根据type改变来使点击的和不点击的产生不同效果 -->
                    </ul>
                </footer>

                <!-- footer --end -->

                <!-- mask --start -->
                <div class="mask-box"
                v-show = "mask_flag"
                @click = "mask_flag = false"
                ><!-- mask_flag控制是否出现黑幕 -->
                    <div class="mask-box-bg"></div>
                    <div class="card">
                        <div class="card-content">
                            <div class="card-content-inner">您确定要删除??</div>
                            <div class="my-btn-box pull-right">
                                <button class="button button-warning button-fill"
                                @click = "remove( active_index )"><!-- 因为处于for循环之外,取不到index值,所以用active_index存下当前index值,此处使用 -->
                                    确定
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- mask --end -->

            </div>
        </div>
    </div>

</body>
<script src="./index.js"></script>

</html>

css代码

*{
    margin: 0;
    padding: 0;
    list-style: none;
}
/* 解决高度塌陷 */
.card{
    overflow: hidden;
}
/* 卡片下方按钮 */
.my-btn-box{
display: flex;
padding: 10px;
}
.my-btn-box button{
    margin: 5px;
}
/* 黑幕 */
.mask-box{
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    z-index:1000;
}
.mask-box-bg{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background: black;
    opacity: .5;
}
/* 黑幕中间盒子 */
.mask-box .card{
    width: 80%;
    height: 100px;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    border-radius: 10px 
}
/* 底部 */
footer{
    position: fixed;
    width: 100%;
    height: 80px;
    left: 0;
    bottom:10px;
}
footer ul{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-around;
}
/* 底部按钮 */
footer ul li{
    width: 80px;
    height: 80px;
    border-radius: 50%;
    /* background: red */
}
footer ul li.circle{
   text-align: center;
   line-height: 80px
}
footer ul li.circle.circle-success{
   border: 1px solid green;
   color: green
}
 footer ul li.circle.circle-success.circle-fill{   
   background: green;
   color: white
}
footer ul li.circle.circle-primary{
    border: 1px solid blue;
    color: blue
 }
  footer ul li.circle.circle-primary.circle-fill{   
    background: blue;
    color: white
 }
 footer ul li.circle.circle-warning{
    border: 1px solid orangered;
    color: orangered
 }
  footer ul li.circle.circle-warning.circle-fill{   
    background: orangered;
    color: white
 }

js代码

new Vue({
    el: '#app',
    data: {/* 原始数据  */
        todos: [
            {
                id : 1,
                task : '唱',
                flag: true/* 用于判断是否勾选 */
            },
            {
                id : 2,
                task : '跳',
                flag: true
            },
            {
                id : 3,
                task : 'rapper',
                flag: true
            }
        ],
        add_todo_flag:false,/* 控制input输入框的出现与消失 */
        add_todo_input:'请输入代办事项',/* input输入框原始提示信息及输入内容 */
        mask_flag:false,/* 控制遮罩的消失与出现 */
        active_index : 0, /* 保存index,在mask点击确定按钮的时候使用 */
        btns:[{
            id:1,
            content:'A',
            name:'all',// all  全部的任务
            mold:'success' //类型
        },{
            id:2,
            content:'F',//finish  完成了的任务
            name:'finish',
            mold:'primary'
        },{
            id:3,
            content:'U',
            name:'unfinish',//unFinish 未完成的任务
            mold:'warning'
        }],
        type:'all'
    },
    methods: {
        check( index ){
            this.active_index = index;
            /* 判断当前是否勾选 */
            if(this.todos[index].flag){
                this.remove(index)
            }else{
                this.mask_flag = true
            }     
        },
        remove( index ){/* 删除当前项 */
            this.todos.splice(index,1)
        },
        addTodoItem () {/* 添加卡片块 */
            this.todos.push({
                id : this.todos.length + 1,
                task : this.add_todo_input,
                flag : true
            });
            this.add_todo_input = '';/* 添加完成后输入框内容变为空 */
            this.add_todo_flag=false;/* 添加完成后输入框消失 */
        },
        getFocus(){/* 获得焦点提示信息消失 */
            this.add_todo_input=''
        }
    },
    computed:{
        allTodos(){
            return this.todos;/* 所有项目 */
        },
        finishTodos(){
            return this.todos.filter(item =>{
                return item.flag && item/* flag为true也就是打了勾的项目 */
            })
        },
        unfinishTodos(){
            return this.todos.filter(item =>{
                return !item.flag && item/* flag为false也就是没打勾的项目 */
            })
        },
        newTodos(){/* 点击不同按钮渲染不同界面 */
            switch (this.type) {
                case 'all':
                    return this.allTodos
                    break;
                case 'finish':
                    return this.finishTodos
                    break;
                case 'unfinish':
                    return this.unfinishTodos
                    break;
                default:
                    break;
            }
        }
    }
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是用Vue制作todolist的步骤: 1. 创建Vue实例 首先需要在HTML文件引入Vue.js库,然后创建一个Vue实例,代码如下: ``` <div id="app"> <input v-model="newTodo" @keyup.enter="addTodo"> <ul> <li v-for="(todo, index) in todos" :key="index"> {{ todo }} <button @click="removeTodo(index)">X</button> </li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script> new Vue({ el: '#app', data: { newTodo: '', todos: [] }, methods: { addTodo() { if (this.newTodo.trim() !== '') { this.todos.push(this.newTodo.trim()); this.newTodo = ''; } }, removeTodo(index) { this.todos.splice(index, 1); } } }); </script> ``` 2. 添加数据和方法 在Vue实例,需要定义数据和方法。数据包括一个用于输入新任务的变量 `newTodo` 和一个用于存储所有任务的数组 `todos`。方法包括添加任务的 `addTodo` 和删除任务的 `removeTodo`。 ``` data: { newTodo: '', todos: [] }, methods: { addTodo() { if (this.newTodo.trim() !== '') { this.todos.push(this.newTodo.trim()); this.newTodo = ''; } }, removeTodo(index) { this.todos.splice(index, 1); } } ``` 3. 绑定数据和事件 在HTML文件,需要使用Vue的指令来绑定数据和事件。使用 `v-model` 指令将输入框和 `newTodo` 变量绑定在一起,使用 `v-for` 指令将任务列表和 `todos` 数组绑定在一起,使用 `@keyup.enter` 指令监听回车键,使用 `@click` 指令监听删除按钮的点击事件。 ``` <input v-model="newTodo" @keyup.enter="addTodo"> <ul> <li v-for="(todo, index) in todos" :key="index"> {{ todo }} <button @click="removeTodo(index)">X</button> </li> </ul> ``` 4. 样式美化 最后,可以对todolist进行样式美化,使其更加美观。 ``` <style> #app { font-family: Arial, sans-serif; margin: 50px auto; max-width: 600px; } input[type="text"] { padding: 10px; border: none; border-bottom: 1px solid #ccc; font-size: 16px; width: 100%; box-sizing: border-box; outline: none; } ul { list-style: none; padding: 0; } li { margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; } button { background-color: #ff4d4d; color: #fff; border: none; border-radius: 50%; height: 30px; width: 30px; font-size: 14px; display: flex; justify-content: center; align-items: center; outline: none; cursor: pointer; } </style> ``` 完成以上四个步骤后,就可以得到一个简单todolist应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值