๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript

๋ชจ๋‹ฌ ํŒ์—… ๋งŒ๋“ค๊ธฐ (240105 ์ฃผ์˜ ์‚ฌํ•ญ ์ถ”๊ฐ€)

 

 

 

๐Ÿ“’ ๋ชจ๋‹ฌ๊ณผ ํŒ์—…, ๋ชจ๋‹ฌํŒ์—…?

ํผ๋ธ”๋ฆฌ์‹ฑ์„ ํ•˜๋‹ค๋ณด๋ฉด ๋ชจ๋‹ฌ๊ณผ ํŒ์—…์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

ํ˜„์žฌ๋Š” ๋ชจ๋‹ฌ๊ณผ ํŒ์—…์ฐฝ์„ ํŠน๋ณ„ํžˆ ๊ตฌ๋ถ„ํ•˜์ง€์•Š์œผ๋ฉฐ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋‹ฌํŒ์—…์ด๋ผ ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค.

 

๊ตฌ๋ถ„ ์‹œ์ผœ๋ณด์ž๋ฉด,

๋ชจ๋‹ฌ์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๊ฒฝ์šฐ ๋œจ๋Š” ์ฐฝ์„ ๋งํ•˜๋ฉฐ

 

ํŒ์—…์€ ํŠน์ • ํŽ˜์ด์ง€์˜ ๋ฐฉ๋ฌธํ•  ๊ฒฝ์šฐ ์ฆ‰์‹œ ๋œจ๋Š” ์ฐฝ์„ ๋œปํ•œ๋‹ค.

 

๊ธฐ์ƒ์ฒญ ๋ฐฉ๋ฌธ์‹œ, ํ˜„์œ„์น˜ ํ™•์ธ์„ ์œ„ํ•œ ๊ถŒํ•œ ์š”์ฒญ ํŒ์—…

 

 

 

 

 

ํŒ์—…์ฐฝ์€ 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.