点此查看 所有教程、项目、源码导航
文章目录
- [1. 开篇介绍与环境引入](#1. 开篇介绍与环境引入)
- [2. 使用方法](#2. 使用方法)
-
- [2.1 基础Toast](#2.1 基础Toast)
- [2.2 自动关闭时长控制](#2.2 自动关闭时长控制)
- [2.3 放置位置](#2.3 放置位置)
- [3. 底层实现关键点解析](#3. 底层实现关键点解析)
- [4. 小结](#4. 小结)
1. 开篇介绍与环境引入
轻提示 Toast 是简短的消息提示,一般在页面角落弹出,自动消失,不强制打断用户操作,常用于操作成功、保存提示。
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title></title>
<!-- Bootstrap 5 CSS -->
<link rel="stylesheet" href="[https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.8/css/bootstrap.min.css](https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.8/css/bootstrap.min.css)">
</head>
<body>
<!-- Bootstrap JS + Popper,用于部分交互 -->
<script src="[https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.8/js/bootstrap.bundle.min.js](https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.8/js/bootstrap.bundle.min.js)"></script>
</body>
</html>
2. 使用方法
2.1 基础Toast
外层 toast,内部 toast-header 头部、toast-body 内容,默认隐藏,需要JS触发显示。
html
<button class="btn btn-primary" id="toastBtn">弹出提示</button>
<div class="toast" id="toastDemo" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">提示</strong>
<small>刚刚</small>
<button type="button" class="btn-close ms-2 mb-1" data-bs-dismiss="toast"></button>
</div>
<div class="toast-body">
操作成功!
</div>
</div>
<script>
// 初始化toast
const toastEl = document.querySelector('#toastDemo');
const toast = new bootstrap.Toast(toastEl);
document.querySelector('#toastBtn').onclick = () => {
toast.show();
}
</script>
效果:

2.2 自动关闭时长控制
构造函数传入 delay,单位毫秒,控制多久自动消失。
html
<button class="btn btn-primary" id="toastBtn2">弹出消失提示</button>
<div class="toast" id="toastDelay" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
1秒后自动消失
</div>
</div>
<script>
const toastEl2 = document.querySelector('#toastDelay');
const toast2 = new bootstrap.Toast(toastEl2, {
delay: 1000
});
document.querySelector('#toastBtn2').onclick = () => {
toast2.show();
}
</script>
2.3 放置位置
用 toast-container 容器配合定位工具类控制位置,例如右下角 position-fixed bottom-0 end-0。
html
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div class="toast" id="toastPos">
<div class="toast-body">我在右下角</div>
</div>
</div>
3. 底层实现关键点解析
下面是精简复刻版本,去掉大量浏览器兼容代码,只保留核心逻辑。
使用上面的HTML代码,加下面自己写的CSS + JS,能得到基本类似的效果,大家可以尝试下。
css
.toast {
/* 默认隐藏 */
display: none;
/* 固定宽高,阴影卡片样式 */
width: 350px;
max-width: 100%;
padding: 0.5rem;
background-color: #fff;
border: 1px solid rgba(0,0,0,0.1);
border-radius: 0.375rem;
box-shadow: 0 0.5rem 1rem rgba(0,0,0,0.15);
/* 透明度过渡动画 */
transition: opacity 0.15s linear;
}
.toast.show {
display: block;
opacity: 1;
}
.toast-header {
/* 头部弹性布局,标题、时间、关闭按钮横向排列 */
display: flex;
align-items: center;
padding: 0.5rem 0.75rem;
color: #6c757d;
background-color: rgba(255,255,255,0.85);
border-bottom: 1px solid rgba(0,0,0,0.05);
}
.toast-body {
/* 内容区域内边距 */
padding: 0.75rem;
color: #212529;
}
.toast-container {
/* 定位容器,用来承载多个toast */
position: absolute;
z-index: 1090;
}
js
// 简易复刻 Toast 核心逻辑,兼容低版本,无ES6+语法
function SimpleToast(el, options) {
var self = this;
this.el = el;
// 默认参数
this.autohide = true;
this.delay = 3000;
this.timer = null;
if (options) {
if (options.autohide !== undefined) {
this.autohide = options.autohide;
}
if (options.delay !== undefined) {
this.delay = options.delay;
}
}
// 找到关闭按钮,绑定点击事件
var closeBtns = this.el.querySelectorAll('[data-bs-dismiss="toast"]');
for (var i = 0; i < closeBtns.length; i++) {
closeBtns[i].onclick = function () {
self.hide();
};
}
}
SimpleToast.prototype.show = function () {
var self = this;
this.el.classList.add('show');
if (this.autohide) {
this.timer = setTimeout(function () {
self.hide();
}, this.delay);
}
};
SimpleToast.prototype.hide = function () {
clearTimeout(this.timer);
this.el.classList.remove('show');
};
// 初始化toast
const toastEl = document.querySelector('#toastDemo');
const toast = new SimpleToast(toastEl);
document.querySelector('#toastBtn').onclick = () => {
toast.show();
}
效果:

4. 小结
- Toast 属于轻量提示,和 Modal 弹窗不一样,不会有遮罩、不会锁定页面滚动。
- 必须通过JS实例化,不能只靠data属性直接弹出;支持
show()/hide()手动控制。 autohide控制是否自动消失,delay设置自动关闭等待时长。- 建议包裹在
toast-container里面,配合定位类控制在右下角、右上角等位置。 - 无障碍属性
aria-live、aria-atomic是给屏幕阅读器读取提示信息用的。 - 多个Toast放在同一个
toast-container里会自动纵向堆叠。