<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제3</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.wrap{
width:400px;
margin:100px auto;
padding:30px;
text-align:center;
border:1px solid #ddd;
border-radius:15px;
}
.heart{
font-size:60px;
color:#999;
cursor:pointer;
transition:0.3s;
}
.heart.active{
color:red;
}
.like{
margin-top:20px;
font-size:24px;
}
</style>
</head>
<body>
<div class="wrap">
<div class="heart">♥</div>
<p class="like">좋아요 <span>0</span>개</p>
</div>
<script>
const heartEl = document.querySelector('.heart');
const countEl = document.querySelector('.like span');
let count = 0;
let isLiked = false;
heartEl.addEventListener('click', () => {
if(isLiked === false){
heartEl.classList.add('active');
count++;
isLiked = true;
}else{
heartEl.classList.remove('active');
count--;
isLiked = false;
}
countEl.innerText = count;
});
</script>
</body>
</html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제3</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.wrap{
width:400px;
margin:100px auto;
padding:30px;
text-align:center;
border:1px solid #ddd;
border-radius:15px;
}
.heart{
font-size:60px;
color:#999;
cursor:pointer;
transition:0.3s;
}
.heart.active{
color:red;
}
.like{
margin-top:20px;
font-size:24px;
}
</style>
</head>
<body>
<div class="wrap">
<div class="heart">♥</div>
<p class="like">좋아요 <span>0</span>개</p>
</div>
<script>
const heartEl = document.querySelector('.heart');
const countEl = document.querySelector('.like span');
let count = 0;
let isLiked = false;
heartEl.addEventListener('click', () => {
if(isLiked === false){
heartEl.classList.add('active');
count++;
isLiked = true;
}else{
heartEl.classList.remove('active');
count--;
isLiked = false;
}
countEl.innerText = count;
});
</script>
</body>
</html>
#문제 3 : 좋아요 버튼 만들기