JSFiddle - React, Tailwind, and code Playground
by twobomb three
HTML
<div class='container'>
<div class="wrap">
<h1>Список задач</h1>
<div class="inp-wrap">
<input class='task' type="text" placeholder='Введите текст задачи'>
<button class='plus'>+</button>
</div>
<div class='task-container' id='task-container'>
</div>
</div>
</div>
CSS
* {
box-sizing:border-box;
}
.container {
max-width: 500px;
margin: 0 auto;
}
h1 {
background-color: orange;
text-align: center;
border-radius:10px 10px 0 0;
color: #fff;
}
.inp-wrap {
margin-bottom: 20px;
display: flex;
padding: 10px;
}
.plus {
margin-left: 15px;
border: none;
border-radius: 20px;
background: lightgreen;
font-size: 30px;
padding: 0 8px;
outline: none;
color: #fff;
&:hover {
color: red;
opacity: 0.8;
}
}
.task {
width: 100%;
padding: 5px;
font-size: 16px;
}
.task-item {
display: flex;
align-items: center;
max-width: 90%;
font-size: 18px;
}
#scales {
margin-right: 10px
}
.task-wrap {
display: flex;
align-items: center;
background: gainsboro;
margin-bottom: 10px;
padding: 10px;
&:nth-child(2n) {
background: #fff;
}
}
.task-wrap-check {
text-decoration: line-through;
}
.minus {
margin-left: auto;
border: none;
border-radius: 20px;
background: lightgreen;
font-size: 25px;
padding: 0 12px;
outline: none;
color: #fff;
&:hover {
color: red;
opacity: 0.8;
}
}
JavaScript
let textVal = document.querySelector('.task')
let btnAdd = document.querySelector('.plus')
let taskCont = document.getElementById('task-container')
let btnDel = document.getElementsByClassName('minus')
let itemsV = taskCont.children
var list = localStorage.getItem('todoList');
if(list == null)
list = [];
else
list = JSON.parse(list);
updateList(list);
function saveTodo(){
localStorage.setItem('todoList',JSON.stringify(list) );
}
function updateList(list){
taskCont.innerHTML = "";
list.forEach((e,i)=>{
let checkClass = e.isCheck?"task-wrap-check":"";
taskCont.innerHTML += `
<div class='task-wrap' >
<div class='task-item ${checkClass}' data-index='${i}'>
${e.value}
</div>
<button class='minus' data-index='${i}'>-</button>
</div>
`;
});
}
function addTask() {
list.push({
value:textVal.value,
isCheck:false
});
textVal.value= "";
updateList(list);
saveTodo();
}
taskCont.addEventListener("click",(e)=>{
if(e.target.classList.contains("task-item")){
list[e.target.getAttribute("data-index")].isCheck = !list[e.target.getAttribute("data-index")].isCheck;
updateList(list);
saveTodo();
}
if(e.target.classList.contains("minus")){
list.splice(e.target.getAttribute("data-index"),1);
updateList(list);
saveTodo();
}
});
btnAdd.addEventListener("click",addTask)