登录页面中悬浮窗口的实现

效果

首先是一个登录页面

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/style.css">
    <!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"> -->
</head>
<body>
    <div class="container">
        <div class="login-page">
            <div class="logo">
                <i class="fas fa-gamepad fa-5x"></i>
            </div>
            <button id="showLoginBtn" class="login-btn">开始游戏</button>
        </div>

        <!-- 登录弹窗 -->
        <div id="loginModal" class="modal">
            <div class="modal-content">
                <span class="close">&times;</span>
                <h2>账号登录</h2>
                <form id="loginForm">
                    <div class="form-group">
                        <input type="text" placeholder="请输入账号" required>
                    </div>
                    <div class="form-group">
                        <input type="password" placeholder="请输入密码" required>
                    </div>
                    <button type="submit" class="login-btn">登录</button>
                </form>
            </div>
        </div>
    </div>
    <script src="js/index.js"></script>
</body>
</html>

给出css样式

/* 全局样式重置 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* 页面主体样式 */
body {
    font-family: 'Microsoft YaHei', sans-serif;
    background: linear-gradient(135deg, #1a1a2e, #16213e);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
}

/* 主容器样式 */
.container {
    width: 100%;
    max-width: 1200px;
    position: relative;
}

/* 登录页面样式 */
.login-page {
    text-align: center;
    padding: 20px;
}

/* Logo区域样式 */
.logo {
    margin-bottom: 30px;
}

/* 游戏图标样式 */
.logo i {
    color: #ff6b6b;
    text-shadow: 0 0 20px rgba(255, 107, 107, 0.6);
    transition: transform 0.3s ease;
}

.logo i:hover {
    transform: scale(1.1);
}

/* 登录按钮样式 */
.login-btn {
    background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
    color: white;
    border: none;
    padding: 15px 40px;
    font-size: 18px;
    border-radius: 25px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(255, 107, 107, 0.4);
}

.login-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(255, 107, 107, 0.6);
}

/* 模态框样式 */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 1000;
    transition: opacity 0.3s ease;
}

/* 模态框内容样式 */
.modal-content {
    background: linear-gradient(135deg, #1a1a2e, #16213e);
    margin: 15% auto;
    padding: 30px;
    width: 90%;
    max-width: 400px;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    position: relative;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* 关闭按钮样式 */
.close {
    position: absolute;
    right: 20px;
    top: 10px;
    font-size: 28px;
    cursor: pointer;
    color: #ff6b6b;
    transition: color 0.3s ease;
}

.close:hover {
    color: #ff8e8e;
}

/* 标题样式 */
h2 {
    text-align: center;
    margin-bottom: 30px;
    color: #ff6b6b;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* 表单组样式 */
.form-group {
    margin-bottom: 20px;
}

/* 输入框样式 */
input {
    width: 100%;
    padding: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.05);
    border-radius: 5px;
    color: white;
    font-size: 16px;
    transition: all 0.3s ease;
}

/* 输入框焦点样式 */
input:focus {
    outline: none;
    border-color: #ff6b6b;
    box-shadow: 0 0 10px rgba(255, 107, 107, 0.3);
}

/* 输入框占位符样式 */
input::placeholder {
    color: rgba(255, 255, 255, 0.5);
}

注释掉JavaScript代码就是一个普通的 登录页面效果 用户点击登录时不会有弹窗效果

 一个普通的登录页面就实现了,但是要想实现登录窗口的悬浮效果就需要用到JavaScript代码

// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 获取DOM元素
    const modal = document.getElementById('loginModal');        // 登录弹窗
    const showLoginBtn = document.getElementById('showLoginBtn'); // 显示登录按钮
    const closeBtn = document.querySelector('.close');          // 关闭按钮
    const loginForm = document.getElementById('loginForm');     // 登录表单

    // 显示登录弹窗
    showLoginBtn.addEventListener('click', function() {
        modal.style.display = 'block';
        // 添加淡入动画效果
        modal.style.opacity = '0';
        setTimeout(() => {
            modal.style.opacity = '1';
        }, 10);
    });

    // 关闭登录弹窗
    closeBtn.addEventListener('click', function() {
        // 添加淡出动画效果
        modal.style.opacity = '0';
        setTimeout(() => {
            modal.style.display = 'none';
        }, 300);
    });

    // 点击弹窗外部关闭
    window.addEventListener('click', function(event) {
        if (event.target === modal) {
            // 添加淡出动画效果
            modal.style.opacity = '0';
            setTimeout(() => {
                modal.style.display = 'none';
            }, 300);
        }
    });

    // 处理登录表单提交
    loginForm.addEventListener('submit', function(e) {
        // 阻止表单默认提交行为
        e.preventDefault();
        
        // 获取表单数据
        const username = this.querySelector('input[type="text"]').value;
        const password = this.querySelector('input[type="password"]').value;
        
        // 这里可以添加登录验证逻辑
        console.log('登录信息:', { username, password });
        
        // 模拟登录成功
        alert('登录成功!');
        
        // 关闭弹窗
        modal.style.opacity = '0';
        setTimeout(() => {
            modal.style.display = 'none';
        }, 300);
    });
});

效果展示

总结

实现基础悬浮登录窗口需要用到的JavaScript代码其实不难,总的来说就几行

// 获取登录标签、关闭标签
let d = document.querySelector("#denglu")
let c = document.querySelector("#guanbi")
// 获取自定义弹窗
let w = document.querySelector(".login-box")
// 点击登录,打开弹窗
d.onclick = function() {
   // 修改自定义弹窗样式
   w.style.display = "block"
}
// 点击关闭,关闭弹窗
c.onclick = function() {
   // 修改自定义弹窗样式
   w.style.display = "none"
}

实现在网页页头

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值