用HTML,css,js实现动态的注册登录页面
时间: 2024-05-14 22:17:19 浏览: 141
好的,我可以给你提供一些思路和代码示例。
首先,我们需要一个基本的 HTML 结构,包括注册和登录两个表单,以及对应的按钮:
```html
<!DOCTYPE html>
<html>
<head>
<title>注册登录页面</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="form-container sign-up-container">
<form action="#">
<h1>创建账户</h1>
<input type="text" placeholder="用户名">
<input type="email" placeholder="邮箱">
<input type="password" placeholder="密码">
<button>注册</button>
</form>
</div>
<div class="form-container sign-in-container">
<form action="#">
<h1>登录</h1>
<input type="email" placeholder="邮箱">
<input type="password" placeholder="密码">
<button>登录</button>
</form>
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>欢迎回来!</h1>
<p>已经有账户?请点击下方按钮登录。</p>
<button class="ghost" id="signIn">登录</button>
</div>
<div class="overlay-panel overlay-right">
<h1>创建账户</h1>
<p>还没有账户?请点击下方按钮注册。</p>
<button class="ghost" id="signUp">注册</button>
</div>
</div>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
```
接下来,我们需要用 CSS 来美化页面,使得它看起来更加漂亮:
```css
* {
box-sizing: border-box;
}
body {
background-color: #f6f5f7;
font-family: 'Open Sans', sans-serif;
}
.container {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 14px 28px rgba(0,0,0,0.25),
0 10px 10px rgba(0,0,0,0.22);
position: relative;
overflow: hidden;
width: 100%;
max-width: 1000px;
min-height: 680px;
}
.form-container {
position: absolute;
top: 0;
height: 100%;
transition: all 0.6s ease-in-out;
}
.sign-in-container {
left: 0;
width: 50%;
z-index: 2;
}
.sign-up-container {
left: 0;
width: 50%;
z-index: 1;
}
.overlay-container {
position: absolute;
top: 0;
left: 50%;
width: 50%;
height: 100%;
overflow: hidden;
transition: transform 0.6s ease-in-out;
z-index: 100;
}
.overlay {
background: #ff416c;
background: -webkit-linear-gradient(to right, #ff4b2b, #ff416c);
background: linear-gradient(to right, #ff4b2b, #ff416c);
background-repeat: no-repeat;
background-size: cover;
background-position: 0 0;
color: #fff;
position: relative;
left: -100%;
height: 100%;
width: 200%;
transition: left 0.6s ease-in-out;
}
.overlay-panel {
position: absolute;
top: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 0 40px;
height: 100%;
width: 50%;
text-align: center;
transform: translateX(0);
transition: transform 0.6s ease-in-out;
}
.overlay-left {
transform: translateX(-20%);
}
.overlay-right {
right: 0;
transform: translateX(0);
}
.form-container.sign-up-container .overlay-left {
transform: translateX(0);
}
.form-container.sign-up-container .overlay-right {
transform: translateX(20%);
}
.overlay-panel h1 {
font-weight: 600;
font-size: 36px;
}
.overlay-panel p {
font-size: 18px;
line-height: 1.5;
margin-top: 20px;
color: #fff;
}
.overlay-panel button {
padding: 12px 36px;
border-radius: 50px;
background-color: transparent;
border: 2px solid #fff;
color: #fff;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
margin: 10px;
}
.overlay-panel button:hover {
background-color: #fff;
color: #ff416c;
}
.form-container.sign-in-container {
z-index: 1;
}
.form-container.sign-up-container {
z-index: 2;
}
.sign-in-container form,
.sign-up-container form {
background-color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
padding: 0 50px;
text-align: center;
}
.sign-in-container input,
.sign-up-container input {
background-color: #eee;
border: none;
padding: 12px 20px;
margin: 8px 0;
width: 100%;
font-size: 16px;
}
.sign-in-container button,
.sign-up-container button {
background-color: #ff416c;
color: #fff;
border: none;
padding: 12px 36px;
border-radius: 50px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
margin: 10px;
}
.sign-in-container button:hover,
.sign-up-container button:hover {
background-color: #fff;
color: #ff416c;
}
```
最后,我们需要编写 JavaScript 代码,使得页面能够实现动态交互。我们可以通过监听按钮的点击事件来实现表单的切换,以及给按钮添加表单验证等功能:
```javascript
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');
signUpButton.addEventListener('click', () => {
container.classList.add('sign-up-mode');
});
signInButton.addEventListener('click', () => {
container.classList.remove('sign-up-mode');
});
// 表单验证
const signUpForm = document.querySelector('.sign-up-container form');
const signInForm = document.querySelector('.sign-in-container form');
signUpForm.addEventListener('submit', (e) => {
e.preventDefault();
checkSignUpInputs();
});
signInForm.addEventListener('submit', (e) => {
e.preventDefault();
checkSignInInputs();
});
function checkSignUpInputs() {
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const confirmPassword = document.getElementById('confirmPassword');
const usernameValue = username.value.trim();
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
const confirmPasswordValue = confirmPassword.value.trim();
if (usernameValue === '') {
setErrorFor(username, '用户名不能为空');
} else {
setSuccessFor(username);
}
if (emailValue === '') {
setErrorFor(email, '邮箱不能为空');
} else if (!isEmail(emailValue)) {
setErrorFor(email, '请输入正确的邮箱地址');
} else {
setSuccessFor(email);
}
if (passwordValue === '') {
setErrorFor(password, '密码不能为空');
} else {
setSuccessFor(password);
}
if (confirmPasswordValue === '') {
setErrorFor(confirmPassword, '确认密码不能为空');
} else if (passwordValue !== confirmPasswordValue) {
setErrorFor(confirmPassword, '密码不一致');
} else {
setSuccessFor(confirmPassword);
}
}
function checkSignInInputs() {
const email = document.getElementById('signInEmail');
const password = document.getElementById('signInPassword');
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
if (emailValue === '') {
setErrorFor(email, '邮箱不能为空');
} else if (!isEmail(emailValue)) {
setErrorFor(email, '请输入正确的邮箱地址');
} else {
setSuccessFor(email);
}
if (passwordValue === '') {
setErrorFor(password, '密码不能为空');
} else {
setSuccessFor(password);
}
}
function setErrorFor(input, message) {
const formControl = input.parentElement;
const small = formControl.querySelector('small');
formControl.className = 'form-control error';
small.innerText = message;
}
function setSuccessFor(input) {
const formControl = input.parentElement;
formControl.className = 'form-control success';
}
function isEmail(email) {
return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(email);
}
```
以上是一个简单的动态注册登录页面的实现,希望能够对你有所帮助!
阅读全文