Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Tạo cửa sổ chat popup với CSS và JavaScript


Chat popup: nút mở chat fixed góc dưới, form chat ẩn/hiện bằng JS.

Ví dụ hoàn chỉnh

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <style>
    body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    }
    * { box-sizing: border-box; }
    .openChatBtn {
      background-color: rgb(123, 28, 179);
      color: white;
      padding: 16px 20px;
      border: none;
      font-weight: 500;
      font-size: 18px;
      cursor: pointer;
      opacity: 0.8;
      position: fixed;
      bottom: 23px;
      right: 28px;
      width: 280px;
      border-radius: 8px;
      z-index: 10;
      transition: opacity 0.2s;
    }
    .openChatBtn:hover { opacity: 1; }
    .openChat {
      display: none;
      position: fixed;
      bottom: 90px;
      right: 28px;
      border: 3px solid #ff08086b;
      border-radius: 12px;
      z-index: 9;
      max-width: 320px;
      width: 100%;
      background: white;
      box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    }
    form { padding: 10px; }
    form textarea {
      width: 100%;
      font-size: 18px;
      padding: 15px;
      margin: 5px 0 22px 0;
      border: 1px solid #ddd;
      border-radius: 8px;
      font-weight: 500;
      background: #f5f5f5;
      color: #333;
      resize: none;
      min-height: 120px;
      box-sizing: border-box;
    }
    form textarea:focus {
      background-color: white;
      outline: 2px solid #7b1cb3;
      border-color: transparent;
    }
    form .btn {
      background-color: rgb(34, 197, 107);
      color: white;
      padding: 14px 20px;
      font-weight: bold;
      border: none;
      border-radius: 8px;
      cursor: pointer;
      width: 100%;
      margin-bottom: 8px;
      opacity: 0.9;
      transition: opacity 0.2s;
    }
    form .btn:hover { opacity: 1; }
    form .close { background-color: #e74c3c; }
  </style>
</head>
<body>
  <h1>Ví Dụ Cửa Sổ Chat Popup</h1>
  <h2>Click nút bên dưới để bắt đầu chat</h2>
  <button class="openChatBtn" onclick="openForm()">💬 Chat</button>
  <div class="openChat" id="chatForm">
    <form>
      <h3 style="margin-top:0">Chat</h3>
      <label for="msg"><b>Tin nhắn</b></label>
      <textarea placeholder="Nhập tin nhắn..." name="msg" required></textarea>
      <button type="submit" class="btn">Gửi</button>
      <button type="button" class="btn close" onclick="closeForm()">Đóng</button>
    </form>
  </div>
  <script>
    const chatBtn = document.querySelector(".openChatBtn");
    const chatForm = document.querySelector(".openChat");
    const closeBtn = document.querySelector(".close");
    
    chatBtn.addEventListener("click", () => chatForm.style.display = "block");
    closeBtn.addEventListener("click", () => chatForm.style.display = "none");
    
    // Đóng khi click ngoài
    document.addEventListener("click", (e) => {
      if (!chatForm.contains(e.target) && !chatBtn.contains(e.target)) {
        chatForm.style.display = "none";
      }
    });
  </script>
</body>
</html>

Kết quả

  • Nút "Chat" cố định góc dưới phải
  • Click mở form chat hiện ra phía trên nút
  • Form có textarea, nút Gửi (xanh), nút Đóng (đỏ)
  • Click ngoài để đóng