프런트엔드-자바스크립트

자바스크립트 핵심

강사: 정프런트 · 작성자: 이종춘 · 작성일: 2026-06-11 21:15:58 · 조회수: 14

문제 4 : 메뉴 클릭 시 모달 열기/닫기
목록으로
<!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>
#문제 4 : 메뉴 클릭 시 모달 열기/닫기

첨부 파일

0개
첨부된 파일이 없습니다.

댓글 / 답글

0개
댓글을 작성하려면 로그인하세요.
아직 댓글이 없습니다.