[jQuery] 모달 레이어 팝업
10 Oct 2021 -
1 minute read
제이쿼리를 이용해 모달 레이어 팝업 구현하기
See the Pen 모달 레이어 팝업 by Jinsol (@losuif) on CodePen.
HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Layer PopUp</title>
<link rel="stylesheet" href="style/style.css">
</head>
<body>
<div id="wrap">
<h1>레이어 팝업 구현</h1>
<div id="noticeLayerPop">
<a href="#">공지사항 팝업 확인 클릭</a>
</div>
</div>
<!-- div#wrap -->
<!-- 레이어 팝업 시작 -->
<div id="layerBG">
<!-- 어두운 불투명 배경 -->
<div id="layerPopup">
<!-- 화면 중앙 출력 영역 -->
<h2>공지사항</h2>
<hr>
<ul>
<li>공지사항 어쩌구</li>
<li>공지사항 저쩌구</li>
<li>어쩌구 저쩌구</li>
</ul>
</div>
<!-- div#layerPopup -->
<div id="closeBtnArea">
×
</div>
</div>
<!-- div#layerBG -->
<script src="lib/jquery-3.6.0.min.js"></script>
<script src="script/script.js"></script>
</body>
</html>
Reset CSS
@charset "UTF-8";
@import url('https://fonts.googleapis.com/css2?family=Nanum+Gothic&display=swap');
/*///////////////////////// CSS 리셋 파일 /////////////////////////*/
* {
color: #222;
font-family: "Nanum Gothic", sans-serif;
text-decoration: none;
padding: 0;
margin: 0;
box-sizing: border-box;
}
a:link {
color: #333;
}
a:visited {
color: #333;
}
a:hover {
color: #444;
}
a:active {
color: #444;
}
img {
vertical-align: middle;
}
ul {
list-style: none;
}
.flex-container {
display: flex;
}
CSS
@charset "UTF-8";
@import url(../style/style_Reset.css);
div#wrap {
width: 800px;
padding: 10px;
margin: 20px auto;
border: 1px solid #000;
}
h1 {
margin: 20px;
}
div#noticeLayerPop a {
font-size: 24px;
text-align: center;
display: block;
margin: 40px;
text-decoration: underline;
}
div#layerBG {
width: 100%;
height: 100%;
/* 모달레이어팝업 > 어두운 배경 */
background-color: rgba(0, 0, 0, 0.55);
/* 어두운 배경의 모달레이어팝업이 아닌
아무 배경이 없는 레이어팝업 */
/* background-color: rgba(255, 255, 255, 0); */
position: fixed;
/* absolute 사용 시 스크롤 내리면 공간 사라짐 */
left: 0;
top: 0;
display: none;
}
div#layerPopup {
width: 400px;
border: 4px solid #aaa;
padding: 20PX;
background-color: #fff;
border-radius: 20px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
div#layerPopup ul {
margin: 20px;
list-style: disc;
}
div#layerPopup ul li {
line-height: 24px;
}
div#closeBtnArea {
/* color: #000;
border: 3px solid #000; */
color: #fff;
font-size: 30px;
font-weight: bold;
user-select: none;
cursor: pointer;
width: 40px;
height: 40px;
border: 3px solid #fff;
border-radius: 50%;
text-align: center;
line-height: 32px;
position: absolute;
right: 20px;
top: 20px;
}
JavaScript
$(function(){
// 모달 레이어 팝업 열기
$("div#noticeLayerPop a").click(function(){
$("div#layerBG").css({"display": "block"});
});
// 모달 레이어 팝업 닫기
$("div#closeBtnArea").click(function(){
$("div#layerBG").css({"display": "none"});
});
});