css
* { box-sizing: border-box; }
body { font-family: Verdana, Geneva, Tahoma, sans-serif; background: cornsilk; margin: 50px; }
button { outline: 0; border: 0; background: transparent; cursor: pointer; }
section { min-width: 360px; max-width: 600px; margin: auto; background: #f1f7ff; border-radius: 15px; box-shadow: 5px 5px 10px rgba(0, 0, 0, 15%); }
header { height: 48px; line-height: 48px; background: rgb(180,205,255); background: linear-gradient(45deg, rgba(180,205,255,1) 0%, rgba(233,179,255,1) 100%); border-radius: 15px 15px 0 0; text-align: center; color: #fff; }
ul.items { height: 500px; overflow-y: auto; padding-bottom: 30px; }
.item_row {
}
.item { display: flex; justify-content: space-between; align-items: center; height: auto; padding: 10px 32px; }
.item_name {
}
.itemDelete_btn { font-size: 18px; transition: .2s ease-in; }
.itemDelete_btn:hover { color: red; transform: scale(1.1); }
.item_divider { width: 90%; height: 1px; margin: auto; background-color: #d7c8ff; }
footer { background: rgb(180,205,255); background: linear-gradient(45deg, rgba(180,205,255,1) 0%, rgba(233,179,255,1) 100%); border-radius: 0 0 15px 15px; text-align: center; }
.f_input { width: 100%; height: 40px; padding: 0 32px; border: 0; outline: none; font-size: 18px; }
.f_input::placeholder { color: #ddd; }
.f_addBtn { width: 48px; height: 48px; font-size: 25px; color: rgba(255, 255, 255, .5); transition: .3s ease-in; }
.f_addBtn:hover { color: rgba(255, 255, 255, 1); transform: scale(1.1); }
html
<section>
<header>ShoppingList</header>
<ul class="items">
<li class="item_row">
<div class="item">
<span class="item_name">์์ด์คํฌ๋ฆผ</span>
<button class="itemDelete_btn">
<i class="fa-solid fa-trash-can"></i>
</button>
</div>
<div class="item_divider"></div>
</li>
</ul>
<footer>
<input type="text" class="f_input" placeholder="enter your shopping list">
<button class="f_addBtn">
<i class="fa-solid fa-circle-plus"></i>
</button>
</footer>
</section>
js
const items = document.querySelector('.items') //ul
const input = document.querySelector('.f_input')
const addBtn = document.querySelector('.f_addBtn')
let id = 0;
let products = [] //์
๋ ฅํ ๋ด์ฉ๋ค ๋ฃ์ ๋ฐฐ์ด
//localStarage์ ์
๋ ฅํ ๋ด์ฉ ์ ์ฅ
const save = () => {
localStorage.setItem('products', JSON.stringify(products));
}
//ํด๋ฆญํ๋ฉด(input์ ์
๋ ฅํ ์ํฐ์น๋ฉด) ๋ฐ์ํ ํจ์ ์ ์
function onAdd(){
const product = {
id:id,
text:input.value
}
products.push(product); //๋ฐฐ์ด products์์ ์ค๋ธ์ ํธ product(์
๋ ฅํ ๋ด์ฉ๊ณผ id)๋ฅผ์ ์ง์ด ๋ฃ๋๋ค
save(); //saveํจ์ ์คํ
if(product.text == ''){
input.focus();
return;
}
//createItem์คํ (ul.items)
createItem(product);
//input ์ด๊ธฐํ
input.value='';
input.focus();
}
//ul.items๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํจ์
function createItem(product){
const itemRow = document.createElement('li');
itemRow.setAttribute('class','item_row');
itemRow.setAttribute('data-id',product.id);
//์คํธ๋ง ๋ฆฌํฐ๋ด ๋ฐฉ์์ผ๋ก ์ถ๊ฐ
itemRow.innerHTML = `
<div class="item">
<span class="item_name">${product.text}</span>
<button class="itemDelete_btn">
<i class="fa-solid fa-trash-can" data-id=${product.id}></i>
</button>
</div>
<div class="item_divider"></div>`;
id++;
//ul.items์ ๋ง๋ ์์ดํ
์ถ๊ฐ
items.appendChild(itemRow)
//์๋ก ์ถ๊ฐ๋ ์์ดํ
์ด ํ๋ฉด์ ๋ณด์ด๊ฒ(์๋์ผ๋ก ์คํฌ๋กค)
itemRow.scrollIntoView();
return items;
//์ต์ข
์ ์ผ๋ก ๋ง๋ค์ด์ค items(ul)๋ฅผ ๋ฆฌํดํด์ค
}
//์ด๊ธฐํ ํด์ฃผ๋ ํจ์(์คํ ๋ฆฌ์ง์ ์ด๋ฏธ ์ ์ฅ๋ ๊ฒ์ด ์์ ๊ฒฝ์ฐ)
function init() {
const userProducts = JSON.parse(localStorage.getItem('products'))
//console.log(userProducts)
if(userProducts) {
userProducts.forEach(aa => createItem(aa))
products = userProducts;
}
}
init();
addBtn.addEventListener('click', onAdd);
input.addEventListener('keypress', event =>{
event.key === "Enter" && onAdd()
});
input.addEventListener('focus',()=>{
input.removeAttribute('placeholder')
})
//์ญ์
items.addEventListener('click',(e)=>{
const idc = e.target.dataset.id; //์ฐ๋ ๊ธฐํต์ ๋๋ ์๋๋ง id๊ฐ ๋ฐ์์ด
if(idc) {
const toBeDeleted = document.querySelector(`.item_row[data-id="${idc}"]`)
toBeDeleted.remove()
// localStorage๋ ์ญ์
products = products.filter((aa) => aa.id !== parseInt(idc)); // html์์ ๊ฐ์ ธ์จ idc๋ ๋ฌธ์์ด์.
save();
}
})