<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제4</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.wrap{
width:400px;
margin:100px auto;
text-align:center;
}
.open_btn{
width:200px;
height:60px;
border:none;
background-color:tomato;
color:white;
font-size:20px;
border-radius:10px;
cursor:pointer;
}
/* 모달 */
.modal_wrap{
width:100%;
height:100vh;
background-color:rgba(0,0,0,0.5);
position:fixed;
top:0;
left:0;
display:none;
justify-content:center;
align-items:center;
}
.show{
display:flex;
}
.modal{
width:350px;
background-color:white;
padding:40px;
border-radius:20px;
text-align:center;
}
.modal h2{
margin-bottom:20px;
}
.close_btn{
width:150px;
height:50px;
border:none;
background-color:black;
color:white;
border-radius:8px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="wrap">
<button class="open_btn">
모달 열기
</button>
</div>
<div class="modal_wrap">
<div class="modal">
<h2>모달 창입니다</h2>
<button class="close_btn">
닫기
</button>
</div>
</div>
<script>
const openBtn = document.querySelector('.open_btn');
const closeBtn = document.querySelector('.close_btn');
const modalWrap = document.querySelector('.modal_wrap');
// 열기
openBtn.addEventListener('click', () => {
modalWrap.classList.add('show');
});
// 닫기
closeBtn.addEventListener('click', () => {
modalWrap.classList.remove('show');
});
</script>
</body>
</html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제4</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.wrap{
width:400px;
margin:100px auto;
text-align:center;
}
.open_btn{
width:200px;
height:60px;
border:none;
background-color:tomato;
color:white;
font-size:20px;
border-radius:10px;
cursor:pointer;
}
/* 모달 */
.modal_wrap{
width:100%;
height:100vh;
background-color:rgba(0,0,0,0.5);
position:fixed;
top:0;
left:0;
display:none;
justify-content:center;
align-items:center;
}
.show{
display:flex;
}
.modal{
width:350px;
background-color:white;
padding:40px;
border-radius:20px;
text-align:center;
}
.modal h2{
margin-bottom:20px;
}
.close_btn{
width:150px;
height:50px;
border:none;
background-color:black;
color:white;
border-radius:8px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="wrap">
<button class="open_btn">
모달 열기
</button>
</div>
<div class="modal_wrap">
<div class="modal">
<h2>모달 창입니다</h2>
<button class="close_btn">
닫기
</button>
</div>
</div>
<script>
const openBtn = document.querySelector('.open_btn');
const closeBtn = document.querySelector('.close_btn');
const modalWrap = document.querySelector('.modal_wrap');
// 열기
openBtn.addEventListener('click', () => {
modalWrap.classList.add('show');
});
// 닫기
closeBtn.addEventListener('click', () => {
modalWrap.classList.remove('show');
});
</script>
</body>
</html>
#문제 4 : 메뉴 클릭 시 모달 열기/닫기