移動 Web App 基於瀏覽器運行,無需安裝、跨平台、更新即時生效。HTML5 提供了原生 App 級別的能力,配合 CSS3 動畫與 JavaScript,可構建體驗接近原生的應用。以下八大特性是核心支柱。
// 保存
localStorage.setItem('token', 'abc123');
sessionStorage.setItem('tabState', JSON.stringify(state));
// 讀取
const token = localStorage.getItem('token');
const state = JSON.parse(sessionStorage.getItem('tabState') || '{}');
// 刪除
localStorage.removeItem('token');
localStorage.clear(); // 清空當前域名下所有
localStorage:持久化,5MB+,跨標籤頁共享sessionStorage:會話級,標籤頁關閉銷毀,隔離性好Cache Manifest 已棄用,現代離線首選 Service Worker + Cache API:
// sw.js
self.addEventListener('install', e => {
e.waitUntil(caches.open('v1').then(c => c.addAll(['/', '/app.js', '/style.css'])));
});
self.addEventListener('fetch', e => {
e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
});
支持:離線首屏加載、靜態資源緩存策略(Cache First/Network First/Stale While Revalidate)、後台同步、推送通知。
<article>
<h1>新聞標題</h1>
<p>正文內容...</p>
<video controls playsinline poster="cover.jpg">
<source src="news.mp4" type="video/mp4">
</video>
<audio controls src="interview.mp3"></audio>
</article>
playsinline 關鍵:iOS Safari 防止全屏播放打斷閱讀流navigator.geolocation.getCurrentPosition(pos => {
const { latitude, longitude } = pos.coords;
// 附近門店、導航、打卡、LBS 社交
}, err => console.warn(err), { enableHighAccuracy: true, timeout: 5000 });
<canvas id="chart" width="600" height="400"></canvas>
<script>
const ctx = document.getElementById('chart').getContext('2d');
// 繪製圖表、遊戲循環、圖像處理
// WebGL 上下文:ctx = canvas.getContext('webgl2') 實現 3D
</script>
canvas.width = cssWidth * devicePixelRatio| API | 能力 | 應用場景 |
|---|---|---|
| Drag & Drop | 拖拽上傳、排序、看板 | 文件上傳、任務看板、編輯器 |
| Clipboard API | 讀寫剪貼板 | 一鍵複製、粘貼圖片上傳 |
| Selection API | 文本選區操作 | 註釋、高亮、分享選中文本 |
| History API | URL 狀態管理 | SPA 路由、瀏覽器前進後退 |
| Touch Events / Pointer Events | 觸控/手勢統一抽象 | 滑動、縮放、長按、手勢識別 |
| Vibration API | 震動反饋 | 遊戲、操作確認、無障礙提示 |
| Screen Orientation / Wake Lock | 屏幕方向鎖定/常亮 | 遊戲、導航、閱讀模式 |
.card {
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.card:active { transform: scale(0.98); }
@keyframes slideIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: none; } }
.animate-in { animation: slideIn 0.4s ease-out both; }
| 模組 | 關鍵特性 | 移動端優勢 |
|---|---|---|
| Selectors L3/L4 | :nth-child、:not()、:has() | 減少 class,結構樣式分離 |
| Fonts | @font-face、font-display、可變字體 | 品牌字體、圖標字體、極小體積 |
| Layout | Flexbox、Grid、Multi-column | 響應式佈局核心 |
| Effects | 圓角、陰影、漸變、濾鏡、混合模式 | 無圖片實現豐富視覺,省帶寬 |
| Animations | Transition、Animation、Keyframes、Motion Path | GPU 加速,60fps 流暢動畫 |
| Logical Properties | margin-inline-start 等 | 原生支持 RTL/LTR 國際化 |
| API | 權限 | 典型用途 |
|---|---|---|
| MediaDevices (getUserMedia) | 攝像頭/麥克風 | 視頻通話、掃碼、拍照上傳、AR |
| Image Capture | 攝像頭 | 高解析度靜態拍照、參數控制 |
| Contacts API (實驗階段) | 通訊錄 | 選擇聯繫人分享、邀請好友 |
| File System Access API | 本地文件 | 文件編輯器、離線文檔處理 |
| Bluetooth / USB / NFC / Serial | 硬件設備 | IoT 控制、硬件調試、支付終端 |
| Device Sensors (Accelerometer, Gyroscope) | 運動傳感器 | 遊戲控制、計步、姿態識別 |
注意:以上多數 API 需 HTTPS、用戶手勢觸發、顯式授權,部分仍在標準化/試驗階段,生產環境需做特性檢測與優雅降級。
HTML5 八大特性構建了移動 Web App 的完整能力矩陣。合理選型、精細優化、漸進增強,Web 技術完全可以支撐商業級移動應用,並擁有跨平台、迭代快、分發輕的獨特優勢。