网站登录功能,弹窗体验更好。点击 登录 弹窗登录界面:

实现:HTML和CSS来完成基本布局和样式,JavaScript实现弹窗的显示和隐藏功能。
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Popup Login</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="overlay" id="overlay">
<div class="popup" id="popup">
<h2>Login</h2>
<form id="loginForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Login</button>
<button type="button" onclick="closePopup()">Cancel</button>
</form>
</div>
</div>
<button onclick="openPopup()">Login</button>
<script src="script.js"></script>
</body>
</html>
CSS代码,保存在styles.css文件中:
body {
font-family: Arial, sans-serif;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: none;
justify-content: center;
align-items: center;
}
.popup {
background: white;
padding: 20px;
border-radius: 5px;
width: 300px;
text-align: center;
position: relative;
z-index: 1;
}
.popup h2 {
margin-top: 0;
}
.popup label {
display: block;
margin: 10px 0 5px;
}
.popup input {
width: calc(100% - 22px);
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
.popup button {
padding: 10px 20px;
border: none;
border-radius: 3px;
cursor: pointer;
background-color: #007BFF;
color: white;
margin: 5px;
}
.popup button:hover {
background-color: #0056b3;
}
Javascript代码,保存在script.js文件:
function openPopup() {
document.getElementById('overlay').style.display = 'flex';
}
function closePopup() {
document.getElementById('overlay').style.display = 'none';
}
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
alert('Login successful!');
closePopup();
});
喜欢记得关注,有问题可留言。

9493

被折叠的 条评论
为什么被折叠?



