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. Vue路由实现之通过URL中的hash(#号)来实现不同页面之间的切换(图表展示、案例分析、附源码详解)

    前言 本篇随笔主要写了Vue框架中路由的基本概念.路由对象属性.vue-router插件的基本使用效果展示.案例分析.原理图解.附源码地址获取. 作为自己对Vue路由进行页面跳转效果知识的总结与笔记. ...

  2. 《HALCON数字图像处理》第六章笔记

    目录 第六章 图像增强 图像增强的概念和分类 灰度变换 直方图处理 图像的平滑 图像的锐化 图像的彩色增强 我在Gitee上建了个仓库,会将学习书本的时候打的一些代码上传上去,笔记中所有代码都在仓库里 ...

  3. 数据库常用DDL语句

    一.创建表 CREATE TABLE TABLE_NAME( #create table 表名 ID INT(4) PRIMARY KEY, #字段名 数据类型 完整性约束条件 NAME VARCHA ...

  4. Myers差分算法的理解、实现、可视化

    作者:Oto_G QQ: 421739728 目录 简介 基础 差异的描述 好的差异比较 算法介绍 名词解释 两个定理 绘制编辑图 感谢 简介 本文章对Myers差分算法(Myers Diff Alg ...

  5. Jmeter(五十三) - 从入门到精通高级篇 - 懒人教你在Linux系统中安装Jmeter(详解教程)

    1.简介 我们绝大多数使用的都是Windows操作系统,因此在Windows系统上安装JMeter已经成了家常便饭,而且安装也相对简单,但是服务器为了安全.灵活小巧,特别是前几年的勒索病毒,现在绝大多 ...

  6. 我所使用的生产 Java 17 启动参数

    JVM 参数升级提示工具:jacoline.dev/inspect JVM 参数词典:chriswhocodes.com Revolut(英国支付巨头)升级 Java 17 实战:https://ww ...

  7. 基于.NET6的开源工业物联网网关

    什么是工业物联网网关 工业物联网网关(IIoTGateway)是一种硬件设备或软件程序,作为本地设备(如PLC.扫码枪.机器人.数控机床.非标上位机等)与云端系统(如物联网平台.SCADA系统.MES ...

  8. 【Java面试】@Resource 和 @Autowired 的区别

    Hi,大家好,我是Mic. 一个工作2年的粉丝,问我一个Spring里面的问题. 希望我能从不同的视角去分析,然后碾压面试官. 这个问题是: "@Resource和@Autowired&qu ...

  9. WPF开发随笔收录-获取软件当前目录的坑

    一.唠唠叨叨 软件开发过程中,经常需要使用到获取exe当前目录这个功能,前同事在实现这个需求时使用的是Directory.GetCurrentDirectory()这个方法,但再最近的测试中,突然发现 ...

  10. NFS网络文件系统搭建

    1. 简介 NFS, 就是network file system的简称. 可以通过NFS, 来共享不同主机的文件.目录. 2010年,NFS已经发展到v4.1版本. 2. 应用场景 在中小型企业中,N ...