頁面右下角滑入彈窗,支持自動關閉、手動關閉、透明度漸變、位置動畫,完美兼容 IE6+/Firefox/Chrome/Safari/Edge。
/* 必須:修復 Firefox 定位基準問題 */
html, body {
height: 100%;
margin: 0;
}
#qq-notify {
position: fixed; /* 相對視口定位,不受滾動影響 */
right: 16px;
bottom: -160px; // 初始隱藏在視口下方
width: 280px;
height: 140px;
border: 1px solid #e0e0e0;
background: #fff;
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
border-radius: 8px;
display: none; // 初始隱藏
z-index: 9999;
opacity: 0;
transition: bottom 0.5s ease, opacity 0.5s ease;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
#qq-notify.show {
display: block;
bottom: 16px;
opacity: 1;
}
.notify-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
border-bottom: 1px solid #f0f0f0;
font-weight: 600;
font-size: 14px;
color: #333;
}
.notify-close {
background: none;
border: none;
font-size: 18px;
color: #999;
cursor: pointer;
padding: 0 4px;
line-height: 1;
}
.notify-close:hover { color: #333; }
.notify-body {
padding: 16px;
font-size: 13px;
color: #666;
line-height: 1.5;
}
.notify-footer {
display: flex;
justify-content: flex-end;
gap: 8px;
padding: 0 16px 12px;
}
.notify-btn {
padding: 6px 16px;
border-radius: 4px;
font-size: 13px;
cursor: pointer;
border: 1px solid #ddd;
background: #fff;
}
.notify-btn.primary {
background: #007bff;
border-color: #007bff;
color: #fff;
}
<div id="qq-notify" class="qq-notify">
<div class="notify-header">
<span class="notify-title">系統通知</span>
<button class="notify-close" aria-label="關閉">×</button>
</div>
<div class="notify-body">
<p class="notify-message">這裡是通知內容...</p>
</div>
<div class="notify-footer">
<button class="notify-btn" data-action="dismiss">稍後查看</button>
<button class="notify-btn primary" data-action="confirm">立即查看</button>
</div>
</div>
class QQNotify {
constructor(options = {}) {
this.options = Object.assign({
container: 'body',
autoClose: 5000, // 自動關閉延遲(ms),0=不自動關閉
animationDuration: 500,
onShow: null,
onHide: null,
onConfirm: null,
onDismiss: null
}, options);
this.$container = $(this.options.container);
this.$el = null;
this.timer = null;
this._render();
}
_render() {
this.$el = $(`
${this._escapeHtml(this.options.title || '通知')}
`).appendTo(this.$container);
this._bindEvents();
}
_bindEvents() {
// 關閉按鈕
this.$el.find('.notify-close, [data-action="dismiss"]').on('click', () => {
this.hide('dismiss');
});
// 確認按鈕
this.$el.find('[data-action="confirm"]').on('click', () => {
this.hide('confirm');
});
// 懸停暫停自動關閉
this.$el.on('mouseenter', () => this._clearTimer());
this.$el.on('mouseleave', () => this._startAutoClose());
}
_escapeHtml(str) {
return String(str).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
}
show(options = {}) {
// 合併選項
Object.assign(this.options, options);
this.$el.find('.notify-title').text(this.options.title || '通知');
this.$el.find('.notify-message').text(this.options.message || '');
this.$el.find('[data-action="dismiss"]').text(this.options.dismissText || '稍後查看');
this.$el.find('[data-action="confirm"]').text(this.options.confirmText || '立即查看');
// 觸發動畫
this.$el.addClass('show');
this._startAutoClose();
this.options.onShow?.(this);
}
hide(action = 'manual') {
this._clearTimer();
this.$el.removeClass('show');
setTimeout(() => {
this.options.onHide?.(this, action);
if (action === 'confirm') this.options.onConfirm?.(this);
else if (action === 'dismiss') this.options.onDismiss?.(this);
}, this.options.animationDuration);
}
_startAutoClose() {
this._clearTimer();
if (this.options.autoClose > 0) {
this.timer = setTimeout(() => this.hide('auto'), this.options.autoClose);
}
}
_clearTimer() {
if (this.timer) clearTimeout(this.timer);
this.timer = null;
}
destroy() {
this._clearTimer();
this.$el?.remove();
this.$el = null;
}
}
// 全局單例管理器
const NotifyManager = {
instances: new Map(),
show(key, options) {
let instance = this.instances.get(key);
if (!instance) {
instance = new QQNotify({ ...options, container: options.container || 'body' });
this.instances.set(key, instance);
}
instance.show(options);
return instance;
},
hide(key, action) {
this.instances.get(key)?.hide(action);
},
destroy(key) {
this.instances.get(key)?.destroy();
this.instances.delete(key);
}
};
// 簡單彈窗
NotifyManager.show('welcome', {
title: '歡迎回來',
message: '您有 3 條新消息,點擊查看',
autoClose: 8000,
onConfirm: () => router.push('/messages')
});
// 帶操作的業務通知
NotifyManager.show('order-123', {
title: '訂單狀態更新',
message: '您的訂單 #20240115001 已發貨,預計明天送達',
confirmText: '查看物流',
dismissText: '我知道了',
autoClose: 0, // 不自動關閉
onConfirm: (inst) => {
window.open('/order/20240115001');
inst.destroy();
}
});
// 批量清理(頁面卸載時)
window.addEventListener('beforeunload', () => {
NotifyManager.instances.forEach((_, key) => NotifyManager.destroy(key));
});
問題: Firefox 以 <html> 為視口基準,若 html 無高度,position: fixed; bottom: -160px 會相對 0 高度定位,導致彈窗初始位置在頂部而非底部。
修復: 設置 html, body { height: 100%; } 確保視口高度正確。
現代方案: 改用 position: fixed 相對視口定位,完全繞過 html/body 高度問題,兼容性更好(IE9+ 全支持)。
role="alert" + aria-live="polite":輔助技術自動朗讀aria-label:關閉按鈕語義化Tab 切換,Enter/Space 確認,Esc 關閉
// 鍵盤支持補充
this.$el.on('keydown', (e) => {
if (e.key === 'Escape') this.hide('dismiss');
if (e.key === 'Enter' && e.target.matches('[data-action="confirm"]')) this.hide('confirm');
});