🕹 스크립트

javascript 모달팝업 / 지정된 날짜까지 보여지기

(。θᗨθ。) 2023. 10. 17. 15:33
<div id="modal" class="modal">
  <div class="modal-content">
    <figure>
      내용
    </figure>
    <div class="button-container">
      <button id="hideTodayBtn">오늘 하루 안보기</button>
      <button id="closeModalBtn">닫기</button>
  </div>
  </div>
</div>
document.addEventListener("DOMContentLoaded", function () {
    const modal = document.getElementById("modal");
    const modalContent = document.querySelector(".modal-content");
    const closeModalBtn = document.getElementById("closeModalBtn");
    const hideTodayBtn = document.getElementById("hideTodayBtn");
    const currentDate = new Date();
    const expirationDate = new Date("2023-12-24");

    if (currentDate <= expirationDate && !localStorage.getItem("hideModal")) {
        modal.style.display = "block";
        document.body.style.overflow = "hidden"; // 비활성화
    }

    closeModalBtn.addEventListener("click", function () {
        modal.style.display = "none";
        document.body.style.overflow = "auto"; // 활성화
    });

    hideTodayBtn.addEventListener("click", function () {
        modal.style.display = "none";
        document.body.style.overflow = "auto"; // 활성화
        const hideUntil = new Date(currentDate);
        hideUntil.setDate(hideUntil.getDate() + 1);
        localStorage.setItem("hideModal", hideUntil.toISOString());
    });

    modal.addEventListener("click", function (e) {
        if (e.target === modal) {
            modal.style.display = "none";
            document.body.style.overflow = "auto"; // 활성화
        }
    });

    document.addEventListener("scroll", function () {
        if (modal.style.display === "block") {
            document.body.style.overflow = "hidden";
        } else {
            document.body.style.overflow = "auto";
        }
    });
  });
  .modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 900;
  }

  .modal-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    text-align: center;
  }



  .modal-content figure {
    height: 80vh;
    overflow-y: auto;
  }


  .modal-content figure img {
    width: 100%;
    height: auto;
  }


  .modal-content figure div {
    background: #F0F0EE;
    padding: 0 3% 3%;
  }


  .modal-content figure div p {
    border: 2px solid #7F53FC;
    text-align: center;
    padding: 3%;
    font-size: 16px;
    font-weight: 900;
  }

  .button-container {
    display: flex;
    justify-content: center;
  }

  .button-container button {
    display: block;
    margin: 0 5px;
    padding: 10px;
    border: 0;
    background: var(--mainBlue);
    color: #fff;
    font-size: 14px;
    font-weight: bold;
    border: 0;
    border-radius: 10px;
    cursor: pointer;
  }


  .button-container button:hover {
    opacity: .7;
  }

  .button-container {
    text-align: center;
    margin-top: 10px;
  }

  .button-container button {
    margin: 5px;
  }


  /* tablet */
  .modal-content figure {
    width: 70vw;
  }

  .modal-content figure div p {
    font-size: 1em;
  }

  /* mobile */
  .btn-more {
    width: 174px;
    height: 44px;
    ;
    font-size: 1rem;
  }

  .modal-content figure {
    height: 56vh;
  }
728x90