如何创建:弹出式表单

学习如何使用 CSS 和 JavaScript 创建弹出表单。

Try It Yourself

如何创建登录表单

第一步 - 添加 HTML:

使用 <form> 元素来处理输入。您可以在我们的 PHP Tutorial 中了解更多信息。

<!-- 用于打开弹出表单的按钮 -->
<button class="open-button" onclick="openForm()">打开表单</button>
<!-- 表单 -->
<div class="form-popup" id="myForm">
  <form action="/action_page.php" class="form-container">
    <h1>登录</h1>
    <label for="email"><b>电子邮件</b></label>
    <input type="text" placeholder="输入电子邮件" name="email" required>
    <label for="psw"><b>密码</b></label>
    <input type="password" placeholder="输入密码" name="psw" required>
    <button type="submit" class="btn">登录</button>
    <button type="button" class="btn cancel" onclick="closeForm()">关闭</button>
  </form>
</div>

第二步 - 添加 CSS:

{box-sizing: border-box;}
/* 用于打开联系表单的按钮 - 固定在页面底部 */
.open-button {
  background-color: #555;
  color: white;
  padding: 16px 20px;
  border: none;
  cursor: pointer;
  opacity: 0.8;
  position: fixed;
  bottom: 23px;
  right: 28px;
  width: 280px;
}
/* 弹出表单 - 默认隐藏 */
.form-popup {
  display: none;
  position: fixed;
  bottom: 0;
  right: 15px;
  border: 3px solid #f1f1f1;
  z-index: 9;
}
/* Add styles to the form container */
.form-container {
  max-width: 300px;
  padding: 10px;
  background-color: white;
}
/* Full-width input fields */
.form-container input[type=text], .form-container input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  border: none;
  background: #f1f1f1;
}
/* Perform some actions when the input field gains focus */
.form-container input[type=text]:focus, .form-container input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}
/* Set styles for submit/login buttons */
.form-container .btn {
  background-color: #04AA6D;
  color: white;
  padding: 16px 20px;
  border: none;
  cursor: pointer;
  width: 100%;
  margin-bottom:10px;
  opacity: 0.8;
}
/* Add red background color to the cancel button */
.form-container .cancel {
  background-color: red;
}
/* Add some hover effects to the buttons */
.form-container .btn:hover, .open-button:hover {
  opacity: 1;
}

Step 3 - Add JavaScript:

function openForm() {
  document.getElementById("myForm").style.display = "block";
}
function closeForm() {
  document.getElementById("myForm").style.display = "none";
}

Try It Yourself

Related Pages

Tutorial:HTML Form

Tutorial:CSS Form