JS的一个小练习

展示成果

话不多说

html骨架

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>待办事项(toDoList)</title>
<link rel="stylesheet" href="Demo.css">
<script src="./Common.js"></script>
<script src="./Demo.js"></script>
</head> <body>
<div class="container">
<header><img src="./title.svg" alt=""><p class="header">待办事项列表</p></header>
<div class="txt">
<input type="text" id="txt" placeholder="添加今日待办事项~~">
<button id="btnAdd">添加</button>
</div>
<div class="liList">
<span class="ev">~今天我需要~</span>
<ul id="ul"> </ul>
</div>
<div class="select">
<p id="count">今日还剩下0个</p>
<button id="all">all</button>
<button id="Active">未完成</button>
<button id="Complete">已完成</button>
<button id="CompleteClear">清楚已完成待办</button>
</div>
</div>
</body> </html>

css样式

@font-face {
font-family: myFont;
src: url(/字体/01223131/今年也要加油鸭-三极字库.ttf);
}
*{
padding: 0;
margin: 0;
box-sizing: border-box;
user-select: none;
}
*:focus{
outline: none;
}
html{
font-family: myFont;
font-size: 18px;
}
body{
display: flex;
background-image: linear-gradient(90deg,#ffafbd,#ffc3a0);
background-repeat: no-repeat;
min-height: 100vh;
}
li{
list-style: none;
}
.container{
display: flex;
flex-direction: column;
padding: 1rem;
margin: 7rem auto;
background-color: #fff;
width: 27rem;
border-radius: 10px;
box-shadow: 4px 5px 15px 0px #999;
justify-content: space-between;
align-self: flex-start;
}
header{
display: flex;
justify-content: flex-start;
}
header>img{
width: 80px;
height: 60px;
margin-left: 5rem;
transform: rotate(-3deg);
}
.header{
width: 161px;
height: 40px;
font-size: 1.3rem;
padding: .25em .8em .15em;
border-radius: 20% 5% 20% 5%/5% 20% 25% 20%;
background-color: #fe7345;
text-align: center;
line-height: 30px;
transform: rotate(3deg);
margin-top: 10px;
color: #fff;
}
.txt{
margin-top: 2rem;
display: flex;
justify-content: space-around;
}
#txt{
width: 18rem;
height: 2rem;
font-size: 1rem;
border: #fe7345 3px dashed;
border-top: none;
border-left: none;
border-right: none;
font-family: myFont;
}
#btnAdd{
width: 100px;
background-color:orange;
border-radius: 47% 42% 17% 24%/28% 39% 52% 43%;
border: none;
transform: rotate(10deg);
transition:all .5s cubic-bezier(.175,.885,.32,1.275);
color: #fff;
font-family: myFont;
}
#btnAdd:hover{
box-shadow: 4px 5px 5px 0px #999;
}
.ev{
font-size: 1.2rem;
display: block;
text-align: left;
margin: .5rem auto;
}
.liList{
margin-top: 1rem;
}
#ul{
padding: 1rem;
border: 1px dashed #eee;
border-radius: 10px;
}
#ul>li{
display: flex;
margin-top: 1rem;
line-height: 2rem;
justify-content: space-between;
border-radius: 5px;
padding: 5px;
}
#ul>li:first-child{
margin-top: 0px;
}
#ul>li>div{
display: flex;
}
#ul>li>div>p{
margin-left: .5rem;
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
-webkit-text-overflow:ellipsis;
}
.btnRemove{
height: 1.5rem;
width: 1.5rem;
border: none;
margin-top: .3rem;
background-color: transparent;
transition: all .3s 0ms;
}
.btnRemove:hover{
background-color: rgba(225, 225, 225, 0.5);
backdrop-filter: blur(4px) saturate(150%);
border-radius: 1.5rem;
}
.select{
display: flex;
}
.select>p{
font-size: .8rem;
margin-top: .7rem;
}
.select>button{
background-color: #fe7345;
border: none;
border-radius: 5px;
margin-left: 15px;
min-width: 30px;
font-size: 1rem;
margin-top: .5rem;
font-size: .9rem;
padding: 3px;
color: white;
font-family: myFont;
} .done{
text-decoration:line-through;
}
.selectItem{
background-color: #fe7345;
color: white;
}
.hidden{
display: none !important;
}
.animate{
animation: animate .75s ease-in-out both;
}
@keyframes animate {
0%{
transform:rotateX(90deg);
opacity: 0;
}
40%{
transform:rotateX(-10deg);
}
70%{
transform:rotateX(10deg);
}
100%{
transform:rotateX(0deg);
opacity: 1;
}
}

原生js

Common.js

function $Id(id) {
return document.getElementById(id);
} function $cls(clsName){
return document.getElementsByClassName(clsName);
} function $tag(tagName){
return document.getElementsByTagName(tagName);
} function $(clsName) {
return document.querySelectorAll(clsName);
} function $one(clsOne) {
return document.querySelector(clsOne);
}
Demo.js

(function (win) {
win.addEventListener('load', () => {
$one('#btnAdd').addEventListener('mousedown', () => {
$one('#btnAdd').style.transform = 'rotate(0deg)';
if ($one('#txt').value.length > 0) {
//获取ul
var ul = $one('#ul');
//创建li
var li = document.createElement('li');
//创建包含复选框和span的div
var liCls = document.createElement('div');
//创建input元素
var checkBox = document.createElement('input');
//创建span显示复选框内容
var p = document.createElement('p');
//创建删除按钮
var btnRemove = document.createElement('button');
//删除按钮添加样式类名
btnRemove.classList.add('btnRemove');
btnRemove.innerText = 'X';
//指定input的类型
checkBox.type = 'checkBox';
//获取文本框内容,并赋值给复选框
checkBox.value = $one('#txt').value;
//赋值给span元素
p.innerText = checkBox.value;
//添加到包含元素
liCls.appendChild(checkBox);
liCls.appendChild(p);
li.appendChild(liCls);
li.appendChild(btnRemove);
ul.appendChild(li);
//清空文本框
$one('#txt').value = '';
checkBox.addEventListener('click', () => {
checkBox.nextSibling.classList.toggle('done');
if (checkBox.checked) {
li.classList.add('selectItem');
btnRemove.classList.add('selectItem');
} else {
li.classList.remove('selectItem');
btnRemove.classList.remove('selectItem');
}
checkBox.parentNode.parentNode.classList.toggle('animate');
CompleteCount();
});
p.addEventListener('mousedown', () => {
if (p.previousSibling.checked) {
p.previousSibling.checked = false;
} else {
p.previousSibling.checked = true;
}
li.classList.toggle('selectItem');
btnRemove.classList.toggle('selectItem');
checkBox.nextSibling.classList.toggle('done');
checkBox.parentNode.parentNode.classList.toggle('animate');
CompleteCount();
});
btnRemove.addEventListener('click', () => {
li.remove();
CompleteCount();
});
CompleteCount();
}
CompleteCount();
});
$one('#btnAdd').addEventListener('mouseup', () => {
$one('#btnAdd').style.transform = 'rotate(10deg)';
});
$one('#btnAdd').addEventListener('mouseleave', () => {
$one('#btnAdd').style.transform = 'rotate(10deg)';
});
$one('#all').addEventListener('click', () => {
for (const li of $('#ul>li>div>p')) {
li.parentNode.parentNode.classList.remove('hidden');
}
});
$one('#Active').addEventListener('click', () => {
for (const li of $('#ul>li>div>p')) {
if (!li.classList.contains('done')) {
li.parentNode.parentNode.classList.remove('hidden');
count++;
} else {
li.parentNode.parentNode.classList.add('hidden');
count--;
}
}
});
$one('#Complete').addEventListener('click', () => {
for (const li of $('#ul>li>div>p')) {
if (!li.classList.contains('done')) {
li.parentNode.parentNode.classList.add('hidden');
} else {
li.parentNode.parentNode.classList.remove('hidden');
}
}
});
$one('#CompleteClear').addEventListener('click', () => {
for (const li of $('#ul>li>div>p')) {
if (li.classList.contains('done')) {
li.parentNode.parentNode.remove();
}
}
});
function CompleteCount() {
var num = 0;
for (const li of $('#ul>li>div>p')) {
if (!li.classList.contains('done')) {
num++;
}
}
$one('#count').innerText = `今日还剩下${num}个`;
}
});
})(window);

转载请声明本文地址https://www.cnblogs.com/snail-wn/articles/16578307.html

js仿toDoList(待办事项)练习的更多相关文章

  1. todolist待办事项

    使用html/css原生js实现待办事项列表: 支持添加待办事项,删除待办事项,切换待办事项的状态(正在进行,已经完成) 支持对正在进行以及已经完成事项编辑(单击内容即可编辑) 源代码:链接:http ...

  2. 基于html实现一个todolist待办事项

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. vue todolist待办事项完整

    <template> <div id="app"> <input type="text" v-model='todo' @keyd ...

  4. vue2.* 事件结合双向数据绑定、模块化以及封装Storage实现todolist 待办事项 已经完成 和进行中持久化 06

    ceshi.vue <template> <div id="app"> <input type='text' v-model='todo' @keyd ...

  5. Vue 事件结合双向数据绑定实现todolist 待办事项 已经完成 和进行中

    <template> <div id="app"> <input type="text" v-model='todo' @keyd ...

  6. vue.js--基础 事件结合双向数据绑定实现todolist 待办事项 已经完成 和进行中,键盘事件

    <template> <div id="app"> <h1>{{ msg }}</h1> <input type=" ...

  7. jQuery模仿ToDoList实现简单的待办事项列表

    功能:在文本框中输入待办事项按下回车后,事项会出现在未完成列表中:点击未完成事项前边的复选框后,该事项会出现在已完成列表中,反之亦然:点击删除按钮会删除该事项:双击事项可以修改事项的内容.待办事项的数 ...

  8. Javascript之网页版待办事项

    本文使用原生JS实现站点 http://www.todolist.cn/ 的基本功能. 其中页面的HTML布局和CSS样式取用原站,JS部分为自己编写. 效果图 完整代码 HTML.JS部分 < ...

  9. 使用vue实现简单的待办事项

    待办事项 效果图 目录结构 详细代码 AddNew.vue <template> <div> <input v-model="content"/> ...

随机推荐

  1. Fastflow——基于golang的轻量级工作流框架

    Fastflow 是什么?用一句话来定义它:一个 基于golang协程.支持水平扩容的分布式高性能工作流框架. 它具有以下特点: 易用性:工作流模型基于 DAG 来定义,同时还提供开箱即用的 API, ...

  2. 爬虫Ⅱ:scrapy框架

    爬虫Ⅱ:scrapy框架 step5: Scrapy框架初识 Scrapy框架的使用 pySpider 什么是框架: 就是一个具有很强通用性且集成了很多功能的项目模板(可以被应用在各种需求中) scr ...

  3. GO GMP协程调度实现原理 5w字长文史上最全

    1 Runtime简介 Go语言是互联网时代的C,因为其语法简洁易学,对高并发拥有语言级别的亲和性.而且不同于虚拟机的方案.Go通过在编译时嵌入平台相关的系统指令可直接编译为对应平台的机器码,同时嵌入 ...

  4. 关闭StackExchange等平台的privacy收集窗口

    技术背景 当我们打开一个StackExchange页面的时候,经常会出现一个很大的privacy收集窗口,而且不管怎么点都关闭不了,比如像下图这样: 如果屏幕足够大,影响可能也不是很大,但是关键是对于 ...

  5. [自制操作系统] 第05回 CPU的三种模式

    目录 一.前景回顾 二.实模式和保护模式 一.前景回顾 在之前我们说到,loader的作用才是读取加载操作系统内核,那么我们的重心就应该是loader.S文件,其实我们接下来也的确是会往loader. ...

  6. vue大型电商项目尚品汇(后台篇)day04

    昨天太晚就没来得及更新,今天是spu管理界面,这个界面一共有三个界面需要切换,完成了两个界面,而且今天的难度在于最后两个章节,富有一定的逻辑性,当然中间也有很多需要注意的,比如ElementUI的照片 ...

  7. BUUCTF-秘密文件

    秘密文件 根据提示得知是属于文件被下载了,查看了下流量包直接过滤ftp包 这里看到有个RAR包存在,应该是隐写了 使用foremost分离即可 得到压缩包存在密码 默认四位纯数字爆破即可 flag{d ...

  8. <%= %> <%- %> <% %>是什么意思?

    .ejs文件后缀的数据渲染,这是服务器端的.把 .html改成 .ejs, (1)<%= %>相当于html中的innerTEXT,导出不包含标签 . (2)<%- %>相当于 ...

  9. UiPath存在图像Image Exists的介绍和使用

    一.Image Exists的介绍 检查是否在指定的UI元素中找到图像,输出的是一个布尔值 二.Image Exists在UiPath中的使用 1. 打开设计器,在设计库中新建一个Sequence,为 ...

  10. ssh-免密钥登陆

    实现openssh免密钥登陆(公私钥验证) 在主机A上,通过root用户,使用ssh-keygen生成的两个密钥:id_rsa和id_rsa.pub 私钥(id_rsa)保存在本地主机,公钥(id_r ...