HTML+CSS+JS 简单登录注册功能页

本文档展示了使用HTML、CSS和JavaScript实现的一个登录、注册和找回密码页面,包括表单验证和本地存储功能。
摘要由CSDN通过智能技术生成

演示视频

https://www.bilibili.com/video/BV1Ui421Z7QP/?share_source=copy_web&vd_source=28c679b91e10365548e98c3b0f7a6fbc

运行页面

  • 登录页

  • 找回密码页面

  • 注册页

代码截图

  • index.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页</title>
    <link rel="stylesheet" href="index.css">
</head>

<body>
    <section>
        <div class=" login-box active">
            <div class="login-form">
                <form action="">
                    <h2>Login</h2>
                    <div class="inputbox">
                        <ion-icon name="person-outline"></ion-icon>
                        <input type="text" id="login-user" required>
                        <label for="">Username</label>
                    </div>
                    <div class="inputbox">
                        <ion-icon name="lock-closed-outline"></ion-icon>
                        <input type="password" id="login-pass" required>
                        <label for="">Password</label>
                    </div>
                    <div class="forget">
                        <label for="remember-me">
                            <input type="checkbox" id="remember-me">Remember Me
                            <a href="#" onclick="goBox('forget')">Forget Password</a>
                        </label>
                    </div>
                    <button class="loginBtn" onclick="doLogin()">Log In</button>
                    <div class="link">
                        <p>Don't have a account
                            <a href="#" onclick="goBox('register')">register</a>
                        </p>
                    </div>
                </form>
            </div>

        </div>
        <div class="register-box">
            <div class="register-form">
                <form action="" id="register-form">
                    <h2>Register</h2>
                    <div class="inputbox">
                        <ion-icon name="person-outline"></ion-icon>
                        <input type="text" id="register-user" required>
                        <label for="">Username</label>
                    </div>
                    <div class="inputbox">
                        <ion-icon name="mail-outline"></ion-icon>
                        <input type="email" id="register-email" required>
                        <label for="">Email</label>
                    </div>
                    <div class="inputbox" style="width: 200px;">
                        <ion-icon name="shield-checkmark-outline"></ion-icon>
                        <input type="text" id="register-code" required>
                        <label for="">Enter Captcha</label>
                        <button onclick="sendCode()">Send Code</button>
                    </div>
                    <div class="inputbox">
                        <ion-icon name="lock-closed-outline"></ion-icon>
                        <input type="password" id="register-pass" required>
                        <label for="">Password</label>
                    </div>

                    <button class="registerBtn" onclick="doRegister()">Register</button>
                    <div class="link">
                        <p>Have a account
                            <a href="#" onclick="goBox('login')">login</a>
                        </p>
                    </div>
                </form>
            </div>
        </div>
        <div class="forget-box">
            <div class="forget-form">
                <form action="" id="forget-form">
                    <h2>Find Password</h2>
                    <div class="inputbox">
                        <ion-icon name="mail-outline"></ion-icon>
                        <input type="email" id="forget-email" required>
                        <label for="">Email</label>
                    </div>
                    <div class="inputbox" style="width: 200px;">
                        <ion-icon name="shield-checkmark-outline"></ion-icon>
                        <input type="text" id="forget-code" required>
                        <label for="">Enter Captcha</label>
                        <button onclick="sendCode()">Send Code</button>
                    </div>
                    <div class="inputbox">
                        <ion-icon name="lock-closed-outline"></ion-icon>
                        <input type="password" id="forget-pass" required>
                        <label for="">New Password</label>
                    </div>
                    <button class="registerBtn" onclick="doUpdate()">Update</button>
                    <div class="link">
                        <p>Have a account
                            <a href="#" onclick="goBox('login')">login</a>
                        </p>
                    </div>
                </form>
            </div>
        </div>
    </section>

    <script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
    <script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
    <script>

        document.addEventListener('DOMContentLoaded', function () {
            // 检查本地存储中的“Remember Me”状态,更新复选框
            let rememberMeCheckbox = document.getElementById('remember-me');

            if (localStorage.getItem('rememberMe') === 'true') {
                rememberMeCheckbox.checked = true;

                let user = JSON.parse(localStorage.getItem('user'))

                document.getElementById('login-user').value=user.username
                document.getElementById('login-pass').value=user.password
            }
        });

        // 登录事件
        function doLogin() {
            const username = document.getElementById('login-user').value
            const password = document.getElementById('login-pass').value
            console.log('登录表单:',
                {
                    username: username,
                    password: password
                });
            if (username === 'sa' && password === '123') {
                
                alert('Login Success!!!')

                const rememberMeCheckBox = document.getElementById('remember-me')
                if (rememberMeCheckBox.checked) {
                    localStorage.setItem('rememberMe', 'true')

                    const userObj = { username: username, password: password };
                    const userStr = JSON.stringify(userObj);
                    localStorage.setItem('user', userStr);
                } else {
                    localStorage.removeItem('rememberMe')
                    localStorage.removeItem('user')
                }


            } else {
                alert('Login Fail!!!')
            }
        }

        // 注册事件
        function doRegister() {
            const username = document.getElementById('register-user').value
            const email = document.getElementById('register-email').value
            const code = document.getElementById('register-code').value
            const password = document.getElementById('register-pass').value


            console.log('注册表单:',
                {
                    username: username,
                    email: email,
                    code: code,
                    password: password
                });

            if (username && email && code && password) {
                alert('Register Success!!!');


                // 清空表单内容
                document.getElementById('register-user').value = '';
                document.getElementById('register-email').value = '';
                document.getElementById('register-code').value = '';
                document.getElementById('register-pass').value = '';

                // 切换到登录表单
                goBox();
            } else {
                alert('Register Fail!!!');
            }
        }

        // 找回密码事件
       function doUpdate() {
            const email = document.getElementById('forget-email').value
            const code = document.getElementById('forget-code').value
            const password = document.getElementById('forget-pass').value


            console.log('更新密码表单',
                {
                    email: email,
                    code: code,
                    password: password
                });

            if (email && code && password) {
                alert('UUpdate Success!!!');


                // 清空表单内容
                document.getElementById('forget-email').value = '';
                document.getElementById('forget-code').value = '';
                document.getElementById('forget-pass').value = '';

                // 切换到登录表单
                goBox();
            } else {
                alert('Update Fail!!!');
            }
        }

        // 切换登录注册表单
        function goBox(type) {
            let loginForm = document.querySelector('.login-box');
            let registerForm = document.querySelector('.register-box');
            let forgetPasswordForm = document.querySelector('.forget-box');

            // 首先隐藏所有表单
            loginForm.classList.remove('active');
            registerForm.classList.remove('active');
            forgetPasswordForm.classList.remove('active');

            // 根据目标显示对应的表单
            if (type === 'register') {
                registerForm.classList.add('active');
            } else if (type === 'forget') {
                forgetPasswordForm.classList.add('active');
            } else {
                loginForm.classList.add('active');
            }
        }

    </script>
</body>

</html>
  •  index.css
* {
    margin: 0;
    padding: 0;
}

section {
    min-height: 100vh;
    width: 100%;
    background: url(../img/bg1.jpg) no-repeat;
    background-position: center;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    position: relative;
}

.login-box,
.register-box,
.forget-box {
    width: 400px;
    height: 400px;
    position: absolute;
    opacity: 0;
    visibility: hidden;
    transform: translateX(50%);
    -webkit-transform: translateX(50%);
    -moz-transform: translateX(50%);
    -ms-transform: translateX(50%);
    -o-transform: translateX(50%);
    transition: all 1s ease-in-out;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -ms-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
}

.register-box {
    height: 550px;
}


.active {
    opacity: 1;
    visibility: visible;
    transform: translateX(0);
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -ms-transform: translateX(0);
    -o-transform: translateX(0);
}

.login-form,
.register-form,
.forget-form {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 400px;
    height: 450px;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(15px);
    border-radius: 20px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    -ms-border-radius: 20px;
    -o-border-radius: 20px;
}

.register-form {
    height: 550px;
    padding: 30px 0 0 0;
}

.forget-form{
    
    padding: 30px 0 0 0;
}


h2 {
    font-size: 2em;
    color: #fff;
    text-align: center;
}

.inputbox {
    position: relative;
    margin: 30px 0;
    width: 310px;
    border-bottom: 2px solid #fff;

    button{
        position: absolute;
        right: -110px;
        top: 8px;
        width: 100px;
        height: 40px;
        font-size: 1em;
        border-radius: 5px;
        background:transparent;
        border: 1px solid #fff;
        color: #fff;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        -ms-border-radius: 5px;
        -o-border-radius: 5px;

        &:hover{
            transform: translateY(-10%);
            -webkit-transform: translateY(-10%);
            -moz-transform: translateY(-10%);
            -ms-transform: translateY(-10%);
            -o-transform: translateY(-10%);
            transition: all 0.3s ease-in-out;
            -webkit-transition: all 0.3s ease-in-out;
            -moz-transition: all 0.3s ease-in-out;
            -ms-transition: all 0.3s ease-in-out;
            -o-transition: all 0.3s ease-in-out;
            box-shadow: 0px 3px 8px -3px #fff;
            
}
}
}

.inputbox label {
    position: absolute;
    top: 50%;
    left: 5px;
    color: #fff;
    font-size: 1em;
    pointer-events: none;
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transition: .5s;
    -webkit-transition: .5s;
    -moz-transition: .5s;
    -ms-transition: .5s;
    -o-transition: .5s;
}

input:focus~label,
input:valid~label {
    top: -5px;
}

.inputbox input {
    width: 100%;
    height: 50px;
    background: transparent;
    border: none;
    outline: none;
    font-size: 1em;
    padding: 0 35px 0 5px;
    color: #fff;
}

.inputbox ion-icon {
    position: absolute;
    right: 8px;
    color: #fff;
    font-size: 1.2em;
    top: 20px;
}

.forget {
    margin: -15px 0 35px;
    font-size: .9em;
    color: #fff;

    label {
        input {
            margin-right: 3px;
            color: #fff;
        }

        a {
            margin-left: 5px;
            font-size: .9em;
            color: #fff;
            text-decoration: none;
            font-weight: 700;
            float: right;

            &:hover {
                text-decoration: underline;
            }
        }
    }
}

.loginBtn,
.registerBtn {
    width: 100%;
    height: 40px;
    border-radius: 40px;
    -webkit-border-radius: 40px;
    -moz-border-radius: 40px;
    -ms-border-radius: 40px;
    -o-border-radius: 40px;
    background: #fff;
    border: none;
    outline: none;
    cursor: pointer;
    font-size: 1em;
    font-weight: 700;
}

.link {
    font-size: .9em;
    color: #fff;
    text-align: center;
    margin: 25px 0 10px;

    a {
        text-decoration: none;
        color: #fff;
        font-weight: 600;
        font-size: .9em;
        margin-left: 5px;

        &:hover {
            text-decoration: underline;
        }
    }
}

创造不易,多多点赞吖

拿走吱一声就行

  • 23
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
你可以使用HTML编写面结构,使用CSS进行样式美化,使用JavaScript实现交互功能。以下是一个简单登录注册面示例代码: ``` <!DOCTYPE html> <html> <head> <title>Login/Register</title> <meta charset="utf-8"> <style> body { font-family: Arial, sans-serif; background-color: #f2f2f2; } form { background-color: #fff; width: 300px; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px #ccc; margin: 0 auto; } h1 { text-align: center; color: #444; } input { margin-bottom: 10px; width: 100%; padding: 10px; box-sizing: border-box; border-radius: 3px; border: 1px solid #ccc; font-family: Arial, sans-serif; font-size: 16px; } button { display: block; margin: 0 auto; padding: 10px 20px; background-color: #0086b3; color: #fff; border: none; border-radius: 3px; font-size: 16px; cursor: pointer; } #register { display: none; } #register-link { text-align: center; margin-top: 10px; font-size: 14px; } #register-link a { color: #0086b3; text-decoration: none; } </style> </head> <body> <form id="login"> <h1>Login</h1> <input type="text" name="username" placeholder="Username" required> <input type="password" name="password" placeholder="Password" required> <button type="submit">Login</button> <div id="register-link">Not registered yet? <a href="#" onclick="showRegister()">Register here</a></div> </form> <form id="register"> <h1>Register</h1> <input type="text" name="username" placeholder="Username" required> <input type="email" name="email" placeholder="Email" required> <input type="password" name="password" placeholder="Password" required> <input type="password" name="password-confirm" placeholder="Confirm password" required> <button type="submit">Register</button> <div id="register-link">Already registered? <a href="#" onclick="showLogin()">Login here</a></div> </form> <script> function showRegister() { document.getElementById("login").style.display = "none"; document.getElementById("register").style.display = "block"; } function showLogin() { document.getElementById("register").style.display = "none"; document.getElementById("login").style.display = "block"; } </script> </body> </html> ``` 你可以将以上代码保存为HTML文件,使用浏览器打开即可看到登录注册面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白白的西柚珉•᷄ࡇ•᷅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值