๐ ๋ชจ๋ฌ๊ณผ ํ์ , ๋ชจ๋ฌํ์ ?
ํผ๋ธ๋ฆฌ์ฑ์ ํ๋ค๋ณด๋ฉด ๋ชจ๋ฌ๊ณผ ํ์ ์ ๊ตฌํํด์ผ ํ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
ํ์ฌ๋ ๋ชจ๋ฌ๊ณผ ํ์ ์ฐฝ์ ํน๋ณํ ๊ตฌ๋ถํ์ง์์ผ๋ฉฐ ํจ๊ป ์ฌ์ฉํ์ฌ ๋ชจ๋ฌํ์ ์ด๋ผ ๋ถ๋ฅด๊ธฐ๋ ํ๋ค.
๊ตฌ๋ถ ์์ผ๋ณด์๋ฉด,
๋ชจ๋ฌ์ ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํด๋ฆญํ ๊ฒฝ์ฐ ๋จ๋ ์ฐฝ์ ๋งํ๋ฉฐ
ํ์ ์ ํน์ ํ์ด์ง์ ๋ฐฉ๋ฌธํ ๊ฒฝ์ฐ ์ฆ์ ๋จ๋ ์ฐฝ์ ๋ปํ๋ค.
ํ์ ์ฐฝ์ alert(), prompt(), confirm() ํจ์๋ฅผ ์ฌ์ฉํด ๊ตฌํํ ์๋ ์๋ค.
- alert()์ ๋จ์ ์๋ฆผ์ฐฝ์ด๋ฉฐ ํ์ธ์ ๋๋ฅด๋ฉด ํด๋น ์ฐฝ์ด ๋ซํ๋ค.
- prompt()์ ๋ด์ฉ์ ์ ๋ ฅํ๋ ํ์ ์ฐฝ์ด๋ค.
- confirm()์ ํ์ธ์ ๋๋ฅด๋ฉด true๊ฐ์ ์ ๋ฌํ๊ณ , ์ทจ์๋ฅผ ๋๋ฅด๋ฉด false๊ฐ์ ์ ๋ฌํ๋ค.
๐ ์์
alert('ํ์
์ฐฝ ์
๋๋ค.')
prompt('์ฑํจ์ ์์ฑํด์ฃผ์ธ์','ํ๊ธธ๋')
// prompt('์ฑํจ์ ์์ฑํด์ฃผ์ธ์') -> ๊ธฐ๋ณธ๊ฐ ์ค์ ์์ผ๋ฉด ์
๋ ฅ๊ฐ์ ์๋ฌด๊ฒ๋ ์์ด ๋์ด
confirm('ํ๊ธธ๋ ๋ณธ์ธ์ด ๋ง์ต๋๊น?')
๐ ์์ - Html
๋ชจ๋ฌ ๊ฒฝ์ฐ ํน์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ๊ธฐ๋ ํ์ง๋ง
์ง์ ๋ง๋ค๊ธฐ๋ ํ๋ค. ์๋๋ ๋ชจ๋ฌ ์์์ด๋ค.
์ผ๋ฐ์ ์ผ๋ก ๋ง์ด ์ฐ์ด๋ <div>๊ฐ ์๋ <aside>๋ฅผ ์ฌ์ฉํ๋ค.
<div class="container">
<button class="btn btn-modal">Open</button>
</div>
<aside class="modal-wrapper">
<div class="modal-wrap">
<div class="modal-head">
<h2 class="modal-title">์ ๋ชฉ์
๋๋ค.</h2>
<button class="btn-close"></button>
</div>
<div class="modal-body">
๋ด์ฉ์
๋๋ค.<br>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit...</p>
</div>
<div class="modal-footer">
<button class="btn btn-no">์๋์ค</button>
<button class="btn btn-yes">์</button>
</div>
</div>
</aside>
๐ ์์ - Css
๋ซ๊ธฐ ๋ฒํผ์ ์์ด์ฝ์ ์ฃผ๋ก ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๋ค.
ํ์ง๋ง conpen์์ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ ์ ์์ด์ ์ง์ ๊ทธ๋ ธ๋ค.
body {
text-align: center;
background-color: #eee;
font-size: 10px;
position: relative;
margin: 0;
}
h1 {
padding-top: 10rem;
font-size: 2rem;
margin: 0;
font-weight: 600;
}
.container {
margin: 0 auto;
max-width: 1200px;
padding: 1rem;
}
.modal-wrapper {
z-index: 99;
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
height: 100vh;
position: absolute;
top: 0;
left: 0;
display: none;
}
.modal-wrapper .modal-wrap {
max-width: 960px;
background-color: #fff;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 999;
width: 100%;
box-shadow: 0px 1px 20px 10px #00000030;
border-radius: 5px;
text-align: left;
}
.modal-wrapper .modal-wrap .modal-head {
border-bottom: 1px solid #ccc;
display: flex;
justify-content: space-between;
align-items: center;
}
.modal-wrapper .modal-wrap .modal-head .modal-title {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
padding: .75rem .75rem .75rem 1rem;
}
.modal-wrapper .modal-wrap .modal-head .btn-close {
padding: 24.5px;
background-color: #eee;
border-left: 1px solid #ccc;
border-top-right-radius: 5px;
position: relative;
cursor: pointer;
}
.modal-wrapper .modal-wrap .modal-head .btn-close:hover {
background-color: #ccc;
}
.modal-wrapper .modal-wrap .modal-head .btn-close::after {
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(-45deg);
position: absolute;
content: '';
display: block;
width: 20px;
height: 2px;
background-color: black;
}
.modal-wrapper .modal-wrap .modal-head .btn-close::before {
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(45deg);
position: absolute;
content: '';
display: block;
width: 20px;
height: 2px;
background-color: black;
}
.modal-wrapper .modal-wrap .modal-body {
padding: 1rem;
font-size: 1rem;
line-height: 1.5;
}
.modal-wrapper .modal-wrap .modal-body p {
height: 1000px;
}
.modal-wrapper .modal-wrap .modal-footer {
padding: 2rem 1rem;
border-top: 1px solid #ccc;
display: flex;
justify-content: center;
gap: 0 1rem;
}
.btn {
background-color: #fff;
width: 120px;
padding: 1rem;
font-size: 1rem;
border: 1px solid #ccc;
border-radius: 5px;
font-weight: 600;
cursor: pointer;
}
.btn-yes {
background-color: rgb(29, 135, 227);
color: #fff;
}
๐ฃ์ฃผ์
์์ ์ฝ๋๋๋ก ์์ฑํ ๊ฒฝ์ฐ,
modal-body์ ๋์ด๊ฐ ๊ธธ์ด์ง๋ก ์๋ ์ฌ์ง๊ณผ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์ด๋ฅผ ๋ฐฉ์งํ๊ณ ์ modal-body์ ๋์ด๋ฅผ ์ง์ ํด์ฃผ๊ณ ์คํฌ๋กค ํ ์์๋๋ก ํ๋ค.
์ถ๊ฐ๋ก ์คํฌ๋กค์ ์ปค์คํ ํด๋ ์ข๋ค.
๐ ์์ - JavaScript
์คํฌ๋ฆฝํธ๋ ์ด์ ์ ์ฌ์ฉํ querySelector() ํจ์๋ก class๋ฅผ ์ง์ ํ๊ณ
style() ํจ์๋ก ์ํ๋ ์คํ์ผ์ ๊ฐ์ ธ์ค๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค.
์ฌ์ฉ๋ฐฉ๋ฒ์
style.์ํ๋ ์คํ์ผ ๋ช .๋ณ๊ฒฝํ๊ณ ์ํ๋ ์์ฑ ์์ผ๋ก ์์ฑํ๋ฉด ๋๋ค.
๋ง์ฝ ์ฌ๋ฌ๊ฐ์ ์คํ์ผ์ ๋ณ๊ฒฝ์ ํ๊ณ ์ถ๋ค๋ฉด style.cssText๋ฅผ ์ฌ์ฉํ๋ค.
const btnModal = document.querySelector('.btn-modal')
const modal = document.querySelector('aside.modal-wrapper');
const btnModalClose = document.querySelector('.modal-wrapper .btn-close');
btnModal.addEventListener("click", () => {
modal.style.display = "block";
});
btnModalClose.addEventListener("click", () => {
modal.style.display = "none";
});
โ ์ต์ข
See the Pen Untitled by yurim99 (@yurim99) on CodePen.
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ClassList ์ด์ ๋ฆฌ (className๊ณผ ๋น๊ต) (0) | 2024.02.05 |
---|---|
์๋์ฐ ์ฐฝ ํฌ๊ธฐ ์์๋ด๊ธฐ (0) | 2024.02.01 |
ํ ๊ธ ๋ฒํผ ๊ตฌํํ๊ธฐ (+ 'X'์ ๋ฒํผ ๋ง๋ค๊ธฐ) (0) | 2024.01.19 |
JS๋ก ๋ง ์ค์ ํ์ ๊ตฌํํ๊ธฐ(+CSS ๊ตฌํ) (1) | 2024.01.09 |
javascript๋ก ํ์ฌ ๋ ์ง์ ์๊ฐ ๊ตฌํ๊ธฐ (0) | 2023.12.26 |