Vue——制作toDoList

简介: 制作toDoList

效果如下:

8f97d0d808bf4d1cafc646fa47d3b713.png

实现代码:

HTML部分:

    <div id="todo" class="todo">
        <div class="header">
            <section>
                <!-- input部分 -->
                <label for="title">ToDoList</label>
                <input type="text" name="title" placeholder="请输入您的待办事项........." required="required" autocomplete="off"
                    v-model="title" v-on:keypress.enter="addTodo">
            </section>
        </div>
        <section>
            <!-- 上半部分 -->
            <h2>上半部分<span id="todoCount" class="count">{{firstList.length}}</span></h2>
            <ol id="todoList">
                <li v-for="(item,index) in firstList">
                    <input type="checkbox" v-on:click.prevent="checkedTodo(index)" v-model="item.done">
                    <p>{{item.title}}</p>
                    <a href="###" v-on:click="deleteABtn1(index)">-</a>
                </li>
            </ol>
            <!-- 下半部分 -->
            <h2>下半部分<span id="doneCount" class="count">{{doneList.length}}</span></h2>
            <ol id="doneList">
                <li v-for="(item,index) in doneList">
                    <input type="checkbox" v-on:click.prevent="checkedDone(index)" v-model="item.done">
                    <p>{{item.title}}</p>
                    <a href="###" v-on:click="deleteABtn2(index)">-</a>
                </li>
            </ol>
        </section>
        <p class="footer">footer</p>
    </div>

CSS部分:

* {
    margin: 0;
    padding: 0;
}
body {
    font-size: 16px;
    background: #cdcdcd;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.header {
    height: 50px;
    background: rgba(47, 47, 47, 0.98);
}
section {
    max-width: 620px;
    margin: 0 auto;
    padding: 0 10px;
}
label {
    float: left;
    width: 100px;
    line-height: 50px;
    color: #ddd;
    font-size: 24px;
    cursor: pointer;
}
.header input {
    float: right;
    width: 60%;
    height: 24px;
    margin-top: 12px;
    text-indent: 10px;
    border-top-left-radius: 5px;
    border-radius: 5px;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.24), 0 1px 6px rgba(0, 0, 0, 0.45) inset;
    outline: none;
    border: none;
}
h2 {
    position: relative;
    margin: 26px 0;
}
h2 .count {
    position: absolute;
    top: 2px;
    right: 5px;
    display: inline-block;
    padding: 0 5px;
    height: 20px;
    border-radius: 20px;
    background: #e6e6fa;
    line-height: 22px;
    text-align: center;
    color: #666;
    font-size: 14px;
}
ol {
    margin: 28px 0 32px;
}
ol,
ul {
    list-style-type: none;
}
li {
    height: 32px;
    line-height: 32px;
    background: #fff;
    position: relative;
    margin-bottom: 10px;
    padding: 0 48px;
    border-radius: 3px;
    border-left: 5px solid #629a9c;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
}
li input {
    position: absolute;
    top: 6px;
    left: 22px;
    width: 22px;
    height: 22px;
    cursor: pointer;
}
li p {
    line-height: 34px;
}
li a {
    position: absolute;
    top: 4px;
    right: 5px;
    display: inline-block;
    width: 14px;
    height: 12px;
    border-radius: 14px;
    border: 6px double #fff;
    background: #ccc;
    line-height: 12px;
    text-align: center;
    color: #fff;
    font-weight: bold;
    font-size: 14px;
    cursor: pointer;
}
#doneList li {
    border-left: 5px solid #999;
    opacity: 0.5;
}
.footer {
    font-size: 14px;
    text-align: center;
    color: #666;
}
@media only screen and (max-width: 400px){
    .header input{
        width: 52%;
    }
}

script部分:

 var todo = new Vue({
            el: "#todo",
            data: {
                title: '',
                // 设置firstList为空数组
                firstList: [],
                // 设置doneList为空数组用来存放到本地
                doneList: [],
            },
            // 载入前:
            beforeMount() {
                // 加载localstorage
                var storage = window.localStorage;
                // 如果获取的todo不为空值时
                if (storage.getItem("todo") !== null) {
                    // 从字符串中解析json对象,并获取数据todo
                    this.firstList = JSON.parse(storage.getItem("todo"));
                }
                if (storage.getItem("done") !== null) {
                    this.doneList = JSON.parse(storage.getItem("done"));
                }
            },
            methods: {
                // 当点击后触发
                addTodo() {
                    // 如果input框里为空则跳出弹窗
                    if (this.title === '') {
                        return alert('您尚未输入需要存储的数据!!');
                    }
                    // 如果有东西那么 上半部分的span中的数值就会将获取到的新数据放在数组的第一个位置
                    this.firstList.unshift({
                        // 让input中的title进行绑定
                        title: this.title,
                        // 把done按钮设置为关闭状态
                        done: false
                    });
                    // 输入完毕后记得清空input框里的值
                    this.title = '';
                    // 把数据存储到本地
                    this.setLocalStorage();
                },
                // 当点击第一部分的删除按钮a时触发该事件
                deleteABtn1(index) {
                    // 把存储过的值进行删除
                    this.firstList.splice(index, 1);
                    // 删除完毕后把剩下的存储到本地
                    this.setLocalStorage();
                },
                // 当点击第二部分的删除按钮a时触发事件
                deleteABtn2(index) {
                    // 点击后可以删除对应的部分
                    this.doneList.splice(index, 1);
                    // 存储一下
                    this.setLocalStorage();
                },
                // 点击第一部分中的input时触发事件
                checkedTodo(index) {
                    // span中的数字的索引的done就为true
                    // firstList.children[0].checked = 'checked'
                    this.firstList[index].done = true;
                    // 把firstList放在doneList的最前面
                    this.doneList.unshift(this.firstList[index]);
                    // 然后上半部分的span的索引也对应删除
                    this.firstList.splice(index, 1);
                    // 并进行存储
                    this.setLocalStorage();
                },
                // 当下半部分出现后的input框(也就是被勾选的框中)  对其设置点击事件
                // 主要是实现点击后将复选框变为关闭状态,并且放在上半部分里,删除本身所在的位置
                checkedDone(index) {
                    // 下半部分的span数字的索引done值设置为flase
                    this.doneList[index].done = false;
                    // 把下半部分放在上半部分大的最前面
                    this.firstList.unshift(this.doneList[index]);
                    // 下半部分的span点击后将删除当前的值
                    this.doneList.splice(index, 1);
                    this.setLocalStorage();
                },
                setLocalStorage() {
                    // 存储localstorage
                    var storage = window.localStorage;
                    // 将 JavaScript 值转换为 JSON 字符串
                    storage.setItem("todo", JSON.stringify(this.firstList));
                    storage.setItem("done", JSON.stringify(this.doneList));
                }
            }
        });
相关文章
|
3月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
493 0
|
3月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
5月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
678 4
|
4月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
502 77
|
5月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
3月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
341 17
|
3月前
|
监控 JavaScript 前端开发
Vue 文件批量下载组件封装完整使用方法及优化方案解析
本文详细介绍了批量下载功能的技术实现与组件封装方案。主要包括两种实现方式:**前端打包方案(基于file-saver和jszip)** 和 **后端打包方案**。前者通过前端直接将文件打包为ZIP下载,适合小文件场景;后者由后端生成ZIP文件流返回,适用于大文件或大量文件下载。同时,提供了可复用的Vue组件`BatchDownload`,支持进度条、失败提示等功能。此外,还扩展了下载进度监控和断点续传等高级功能,并针对跨域、性能优化及用户体验改进提出了建议。可根据实际需求选择合适方案并快速集成到项目中。
329 17
|
3月前
|
JavaScript 前端开发 UED
Vue 手风琴实现的三种常用方式及长尾关键词解析
手风琴效果是Vue开发中常见的交互组件,可节省页面空间、提升用户体验。本文介绍三种实现方式:1) 原生Vue结合数据绑定与CSS动画;2) 使用Element UI等组件库快速构建;3) 自定义指令操作DOM实现独特效果。每种方式适用于不同场景,可根据项目需求选择。示例包括产品特性页、后台菜单及FAQ展示,灵活满足多样需求。附代码示例与资源链接,助你高效实现手风琴功能。
144 10
|
3月前
|
JavaScript 前端开发 UED
Vue 表情包输入组件的实现代码:支持自定义表情库、快捷键发送和输入框联动的聊天表情解决方案
本文详细介绍了在 Vue 项目中实现一个功能完善、交互友好的表情包输入组件的方法,并提供了具体的应用实例。组件设计包含表情分类展示、响应式布局、与输入框的交互及样式定制等功能。通过核心技术实现,如将表情插入输入框光标位置和点击外部关闭选择器,确保用户体验流畅。同时探讨了性能优化策略,如懒加载和虚拟滚动,以及扩展性方案,如自定义主题和国际化支持。最终,展示了如何在聊天界面中集成该组件,为用户提供丰富的表情输入体验。
283 8
|
3月前
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
186 1