如何制作类似Discord的登录表单,解决层级与气泡消失问题
解决方案
1. 修复z-index不生效问题
z-index仅对position属性值为relative/absolute/fixed/sticky的元素生效,你可以按以下规则调整层级:
- 给登录表单添加
position: fixed(或者其他非static定位属性),同时设置z-index: 999,保证层级最高 - 确认星空、波浪、气泡层都设置了非static定位,z-index依次设置为1、2、3,都低于表单的z-index值
- 不要把登录表单嵌套在z-index较低的父容器内,建议直接作为body的直接子元素,避免父容器的层叠上下文限制子元素层级
2. 修复微气泡消失问题
微气泡消失通常是被上层元素遮挡,或者生成气泡的容器层级被压低导致:
- 单独给微气泡容器设置
z-index: 3,保证它在波浪层之上、登录表单之下 - 检查是否给微气泡的父容器设置了
overflow: hidden,如果有则移除,避免气泡超出容器范围后被裁剪 - 登录表单可以保留低透明度效果让下层气泡隐约可见,更贴近Discord的原生设计
参考代码示例
HTML结构调整
<!DOCTYPE html> <head> <link rel="stylesheet" href="css/waves.css"> <link href='https://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'> </head> <body> <!-- 背景层 --> <div id='stars'></div> <div id='stars2'></div> <div id='stars3'></div> <div class="waves-container"></div> <div class="bubble-container"></div> <!-- 登录表单层 --> <div class="login-box"> <h2>欢迎回来</h2> <div class="input-group"> <label>邮箱或手机号</label> <input type="text" placeholder="请输入邮箱或手机号"> </div> <div class="input-group"> <label>密码</label> <input type="password" placeholder="请输入密码"> </div> <button class="login-btn">登录</button> </div> <script src="waves.js"></script> </body>
核心CSS配置
/* 登录表单样式 */ .login-box { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 480px; padding: 32px; background: #36393f; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); z-index: 999; color: #fff; font-family: 'Lato', sans-serif; } .input-group { margin-bottom: 20px; } .input-group label { display: block; margin-bottom: 8px; font-size: 14px; font-weight: 700; text-transform: uppercase; } .input-group input { width: 100%; height: 40px; padding: 10px; border: 1px solid #202225; border-radius: 4px; background: #202225; color: #fff; font-size: 16px; } .login-btn { width: 100%; height: 44px; border: none; border-radius: 4px; background: #5865f2; color: #fff; font-size: 16px; font-weight: 500; cursor: pointer; } /* 背景层层级配置 */ #stars, #stars2, #stars3 { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 0; } .waves-container { position: fixed; bottom: 0; left: 0; width: 100%; z-index: 1; } .bubble-container { position: fixed; bottom: 0; left: 0; width: 100%; z-index: 2; }
内容的提问来源于stack exchange,提问作者Shree Krishna
相关产品推荐
相关产品推荐

