You need to enable JavaScript to run this app. HTML中如何重叠两个div实现登录成功居中弹出消息提示?
优惠活动
大模型
产品
解决方案
定价
更多

HTML中如何重叠两个div实现登录成功居中弹出消息提示?

最简实现方案(原生HTML/CSS/JS,无需依赖第三方库)

只用几十行代码就能实现你要的居中消息提示,比调用alert体验好很多,也不需要引入额外依赖:

  • 第一步:添加HTML基础结构
    直接在登录页面body末尾加一段提示框结构,默认隐藏即可:
<!-- 消息提示遮罩层 -->
<div id="successTip" class="tip-modal">
  <div class="tip-content">
    <h3>登录成功</h3>
    <p>欢迎回来,正在为您跳转至个人中心...</p>
    <button class="tip-close">知道了</button>
  </div>
</div>
  • 第二步:添加CSS样式实现居中、美化效果
/* 遮罩层默认隐藏 */
.tip-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0,0,0,0.5);
  z-index: 9999;
}
/* 提示框居中核心逻辑 */
.tip-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  padding: 24px 32px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  text-align: center;
  min-width: 300px;
}
.tip-close {
  margin-top: 16px;
  padding: 6px 18px;
  border: none;
  background: #1677ff;
  color: #fff;
  border-radius: 4px;
  cursor: pointer;
}
/* 显示控制类 */
.tip-modal.show {
  display: block;
}
  • 第三步:添加JS触发逻辑
    登录接口请求成功之后直接调用显示逻辑即可,还可以按需添加自动关闭功能:
// 登录成功后执行这段代码
const tip = document.getElementById('successTip')
// 显示提示
tip.classList.add('show')
// 点击关闭按钮隐藏提示
tip.querySelector('.tip-close').addEventListener('click', () => {
  tip.classList.remove('show')
})
// 可选配置:3秒后自动关闭提示
setTimeout(() => {
  tip.classList.remove('show')
}, 3000)

如果你的项目已经引入了Element UI、Ant Design这类前端UI库,直接调用内置的全局消息方法即可,一行代码就能实现,默认支持居中配置,不需要额外写样式。

内容的提问来源于stack exchange,提问作者Moritz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 17:15:05