`
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>模态框</title><link rel="stylesheet" href="作业.css"></head><body><header><h1 class="title">xi-blog</h1><button onclick="showmodal()">登录</button></header><div class="modal"><div class="modal-bg" onclick="closemodal()"></div><form action="" class="modal-form"><fieldset style="display: grid; gap: 1em"><legend>登录</legend><input type="text" name="user" placeholder="请输入用户名" required><input type="email" name="email" placeholder="请输入邮箱" required><input type="password" name="password" placeholder="请输入密码" required><button>登录</button><button>注册</button></fieldset></form></div><script src="modal.js"></script></body></html>
*{margin: 0;padding: 0;box-sizing: border-box;}header{background-color: lightgreen;padding: 1em 1em;display: flex;}header .title{font-size:x-large;font-style: italic;letter-spacing: 1px;color: lightseagreen;text-shadow: 1px 1px 1px black;}header button{margin-left: auto;border: none;background-color: lightseagreen;color: greenyellow;width: 5em;border-radius: 1em;}header button:hover{cursor: pointer;background-color: black;color: white;box-shadow: 0 0 3px black;transition: 0.3s;}.modal .modal-form fieldset{padding: 2em 2em;background-color: lightgreen;height: 19em;border: 0;box-shadow: 0 0 10px lightseagreen;}.modal .modal-form fieldset legend{background-color: lightseagreen;color: rebeccapurple;padding: 0.5em 1em;}.modal .modal-form fieldset input{padding-left: 1em;font-size: 0.7em;outline: none;height: 3em;}.modal .modal-form fieldset input:focus{background-color: black;color: white;border: 0;box-shadow: 0 0 2px red;}.modal .modal-form fieldset button{background-color:black;color: white;border: 0;height: 3em;font-size: 10px;}.modal .modal-form fieldset button:hover{cursor: pointer;background-color: white;color: black;}/*设置表单组件部分的参数设置为绝对定位设置具体的定位参数(让其随浏览器变化)*/.modal .modal-form{position: fixed;top: 20%;left: 30%;right: 30%;}/*设置半透明遮罩参数设置绝对定位定位到四个角 (全覆盖)颜色设置为半透明*/.modal .modal-bg{position: fixed;top: 0;left: 0;bottom: 0;right: 0;background-color: rgb(0,0,0,0.5);}/*将整个模态框隐藏*/.modal{display: none;}
function showmodal() {// 获取模态框元素const modal = document.querySelector('.modal');// 显示模态框modal.style.display = 'block';// 焦点自动置入第一个输入框emailmodal.querySelector('input:first-of-type').focus();}function closemodal() {// 获取模态框元素const modal = document.querySelector('.modal');// 关闭模态框modal.style.display = 'none';}
相关推荐
© 2020 asciim码
人生就是一场修行