modal UI

by dfg312546

HTML

<button onclick="loginModal()">點我登入帳號</button>
<div class="modal" id="login-modal">
  <div>帳號:<input type="text"></div>
  <div>密碼:<input type="password"></div>
  <br>
  <button onclick="closeModal()" class="btn-close">關閉</button>
</div>

<hr>

<button onclick="postModal()">點我新增貼文</button>
<div class="modal" id="post-modal">
  <div>標題:<input type="text"></div>
  <div>
  內文:<br>
    <textarea></textarea>
  </div>
  <button onclick="closeModal()" class="btn-close">關閉</button>
</div>

<div class="overlay" id="overlay"></div>

CSS

button {
  border-radius:7px;
  border:thin;
  padding:5px;
}

button:hover {
  color:white;
  background-color:black;
}

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 0;
  display: none;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  z-index: 1;
  display: none;
}

JavaScript

function loginModal() {
  document.getElementById('overlay').style.display = 'block';
  document.getElementById('login-modal').style.display = 'block';
}

function postModal() {
  document.getElementById('overlay').style.display = 'block';
  document.getElementById('post-modal').style.display = 'block';
}

function closeModal() {
  document.getElementById('overlay').style.display = 'none';
  event.currentTarget.parentElement.style.display = 'none';
}