萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

hiAlert:基於 jQuery 的輕量級彈窗美化插件使用指南與現代原生替代方案

hiAlert 簡介

  • 基於 jQuery 1.7+,約 8 KB (gzipped: 3 KB)
  • 五大方法:`hiAlert`、`hiConfirm`、`hiPrompt`、`hiBox`、`hiOverAlert`
  • 支援拖曳 (需 `jquery.ui.draggable`)、主題定制、回調函數
  • 兼容 IE6+、Chrome、Firefox、Safari、Opera

快速開始

<link rel="stylesheet" href="hiAlert.css">
<script src="jquery.min.js"></script>
<script src="jquery.hiAlert.js"></script>

<script>
hiAlert('歡迎來到 helloweba.com', '提示');
hiConfirm('確認刪除?', '確認', r => { if (r) console.log('已刪除'); });
hiPrompt('請輸入名字', '預設值', '輸入', r => { if (r) console.log(r); });
hiBox('#hiddenDiv', '標題', 400, 300, '', '.close-btn');
hiOverAlert('操作成功!', 2000); // 頂部漸變提示條
</script>

參數詳解

方法參數說明
hiAlertcontent提示內容 (HTML 字串)
title標題 (可選)
callback關閉後回調 `function()` (可選)
hiConfirmcontent提示內容
title標題
callback`function(confirmed: boolean)` 點確定/取消
hiPromptcontent提示內容
defaultVal輸入框預設值
title標題
callback`function(value: string|null)` 取消返回 null
hiBoxobj選擇器/元素:彈窗內容容器 (需隱藏在頁面)
title標題
w寬度 (px)
h高度 (px/'' 自適應)
submit提交按鈕選擇器 (可選)
cancel取消/關閉按鈕選擇器 (可選)
callback`function(data)` 提交時回調
hiOverAlertcontent提示條內容
timeout自動關閉毫秒數 (預設 3000)

現代原生替代方案:`Dialog` 元素 + `Popover` API (零依賴)

<!-- HTML -->
<dialog id="alertDlg">
  <form method="dialog">
    <p id="alertMsg"></p>
    <menu><button value="ok" autofocus>確定</button></menu>
  </form>
</dialog>

<dialog id="confirmDlg">
  <form method="dialog">
    <p id="confirmMsg"></p>
    <menu>
      <button value="cancel">取消</button>
      <button value="ok" id="confirmOk" autofocus>確定</button>
    </menu>
  </form>
</dialog>

<style>
dialog { border: none; border-radius: 8px; box-shadow: 0 12px 32px rgba(0,0,0,.2); padding: 0; max-width: 90vw; }
dialog::backdrop { background: rgba(0,0,0,.4); backdrop-filter: blur(2px); }
dialog[open] { animation: fadeIn .2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: scale(.95); } to { opacity: 1; transform: scale(1); } }
dialog form { padding: 24px; display: flex; flex-direction: column; gap: 16px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 0; padding: 0 24px 24px; }
dialog button { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; }
button[value="ok"] { background: #1976d2; color: #fff; }
button[value="cancel"] { background: #e0e0e0; }
</style>

<script>
// Alert
function alert(message, title = '提示') {
  const dlg = document.getElementById('alertDlg');
  dlg.querySelector('#alertMsg').textContent = message;
  return dlg.showModal(); // 返回 Promise,關閉後 resolve(returnValue)
}

// Confirm
function confirm(message, title = '確認') {
  const dlg = document.getElementById('confirmDlg');
  dlg.querySelector('#confirmMsg').textContent = message;
  return dlg.showModal().then(v => v === 'ok');
}

// Prompt
function prompt(message, defaultVal = '', title = '輸入') {
  // 需自建含 input 的 dialog,略
}

// Toast / OverAlert (頂部提示條)
function toast(message, ms = 3000) {
  const el = document.createElement('div');
  el.textContent = message;
  el.style.cssText = 'position:fixed;top:20px;left:50%;transform:translateX(-50%);background:#333;color:#fff;padding:12px 24px;border-radius:8px;z-index:9999;animation:slideDown .3s ease';
  document.body.appendChild(el);
  setTimeout(() => { el.style.animation = 'slideUp .3s ease forwards'; el.addEventListener('animationend',()=>el.remove()); }, ms);
}

// 使用
alert('歡迎!').then(() => console.log('已關閉'));
confirm('確定刪除?').then(ok => ok && console.log('刪除'));
toast('保存成功', 2000);
</script>

Popover API (Chrome 114+, Edge 114+, Safari 17+)

<button popovertarget="myPopover">打開</button>
<div id="myPopover" popover>
  內容區域
  <button popovertarget="myPopover" popovertargetaction="hide">關閉</button>
</div>

<style>
[popover] { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); border: none; border-radius: 8px; box-shadow: 0 12px 32px rgba(0,0,0,.2); padding: 24px; background: white; }
[popover]::backdrop { background: rgba(0,0,0,.4); }
</style>

零 JS、瀏覽器原生管理焦點陷阱、ESC 關閉、背景遮罩、動畫 (配合 `@starting-style`)。

對比總結

方案體積依賴瀏覽器支援功能完整度推薦場景
hiAlert8 KBjQuery + jQuery UI (拖曳)IE6+完整 (5 方法)老專案、需 IE 支援、快速接入
`` + JS~1 KB無Chrome 37+, FF 53+, Safari 15.4+完整 (焦點陷阱、模態、表單關閉)**現代專案首選**
Popover API0 KB (純 HTML/CSS)無Chrome 114+, Edge 114+, Safari 17+非模態、輕量提示、菜單、Tooltip非模態浮層、下拉選單
SweetAlert240 KB無 (可選 jQuery)現代瀏覽器極豐富 (圖標、動畫、隊列、主題)需華麗視覺、複雜交互

結語

hiAlert 在 jQuery 時代是優秀的輕量彈窗方案。現代專案**強烈建議遷移到原生 `

` + `Popover API`**:零依賴、語意化、瀏覽器原生處理焦點/無障礙/動畫、體積極小。僅在必須支援 IE/舊版 WebView 時才考慮 hiAlert 或 SweetAlert2 等 Polyfill 方案。

copyright © 萬盛學電腦網 all rights reserved