今天要介紹三個 Web API——Screen Orientation、Wake Lock、Vibration——很有趣! ✨✨✨ 因為它們把「原生 App 的感覺」搬進瀏覽器:
它們特別適合應用在手機瀏覽器上(Android 效果最明顯),但在桌面端也有一定的用武之地(例如展示畫面、Kiosk、教學現場的 Demo)。整體來說:
API | 典型用途 | 是否需要使用者手勢 | 常見限制 | 備註 |
---|---|---|---|---|
Screen Orientation | 鎖定螢幕直向/橫向、監聽旋轉變化 | 鎖定通常需要手勢觸發;部分瀏覽器需全螢幕 | 支援度依裝置/瀏覽器而異;iOS 歷史上受限較多 | 可用 @media (orientation) 做 CSS 兜底處理 |
Wake Lock (Screen) | 保持螢幕不進入睡眠模式 | 通常需要手勢觸發 | 失焦或螢幕鎖定會被釋放;需 HTTPS | 記得監聽 visibilitychange 重新申請 |
Vibration | 按鈕回饋、提醒、簡易節奏 | 通常需要手勢觸發 | iOS Safari 傳統上不支援;桌面多半無回饋 | 節制使用,顧及無障礙與使用者環境 |
注意事項:
- 通常用 使用者手勢 才能成功。
- 支援度不穩定,一定要有 失敗降級 與 try/catch。
screen.orientation.type
(ex: 直向模式 portrait-primary
、橫向模式 landscape-primary
)。screen.orientation.onchange
。await screen.orientation.lock('landscape')
、unlock()
。提醒:有些瀏覽器規定,只有在 全螢幕模式 下或在 使用者有點擊、觸控等手勢 時,才允許鎖定螢幕方向。如果直接用程式碼鎖定,沒有符合這些條件,瀏覽器會拒絕並拋出 DOMException 錯誤。
<button type="button" id="lock-landscape">鎖定橫向</button>
<button type="button" id="unlock">解除鎖定</button>
<script type="module">
// 讀取目前螢幕方向 & 角度
console.log('type:', screen.orientation?.type, 'angle:', screen.orientation?.angle);
// 監聽螢幕方向改變(新)
screen.orientation?.addEventListener?.('change', () => {
console.log('changed:', screen.orientation.type, screen.orientation.angle);
});
// 嘗試鎖定為橫向(需使用者手勢;部分瀏覽器可能會被拒絕)
async function lockLandscape() {
try {
await screen.orientation?.lock?.('landscape');
} catch (e) {
console.warn('lock failed:', e?.name || e);
}
}
// 解除鎖定
function unlockOrientation() {
try {
screen.orientation?.unlock?.();
} catch {
console.warn('unlock failed');
}
}
// 綁定手勢
document.getElementById('lock-landscape')?.addEventListener('click', lockLandscape);
document.getElementById('unlock')?.addEventListener('click', unlockOrientation);
</script>
/* 直向時調整比例 */
@media (orientation: portrait) {
.video {
width: 100%;
height: 56.25vw;
}
}
/* 橫向時調整比例 */
@media (orientation: landscape) {
.video {
width: 56.25vh;
height: 100%;
}
}
unlock()
。const lock = await navigator.wakeLock.request('screen')
。lock.addEventListener('release', ...)
。<button type="button" id="wk-on">保持常亮</button>
<button type="button" id="wk-off">釋放常亮</button>
<script type="module">
let wakeLock = null;
// 使用者是否已要求保持常亮
let keepAwakeRequested = false;
// 申請螢幕常亮(需 HTTPS + 使用者手勢)
async function requestWakeLock() {
keepAwakeRequested = true;
try {
wakeLock = await navigator.wakeLock?.request?.('screen');
// 當 Wake Lock 被釋放(例如切換分頁、裝置鎖定)時觸發
wakeLock?.addEventListener('release', () => { wakeLock = null; });
} catch (e) {
console.warn('request failed:', e?.name || e);
}
}
// 釋放(取消)螢幕常亮
async function releaseWakeLock() {
keepAwakeRequested = false;
try {
await wakeLock?.release();
} finally {
wakeLock = null;
}
}
// 回到可見時,如使用者曾要求保持常亮且已被釋放,則再嘗試一次
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible' && keepAwakeRequested && !wakeLock) {
requestWakeLock();
}
});
document.getElementById('wk-on')?.addEventListener('click', requestWakeLock);
document.getElementById('wk-off')?.addEventListener('click', releaseWakeLock);
</script>
navigator.vibrate(200)
(毫秒)。navigator.vibrate([100, 50, 100, 200])
(震動-停頓-震動…)。navigator.vibrate(0)
。<button id="btn-tap">輕觸回饋</button>
<button id="btn-success">成功提示</button>
<button id="btn-stop">停止震動</button>
<script type="module">
const vibrate = (pattern) => navigator.vibrate?.(pattern) ?? false;
// 輕微震動一下
document.querySelector('#btn-tap').addEventListener('click', () => {
vibrate(30);
});
// 嘀-停-嘀(成功感)
document.querySelector('#btn-success').addEventListener('click', () => {
vibrate([40, 40, 80]);
});
document.querySelector('#btn-stop').addEventListener('click', () => vibrate(0));
</script>
上面用簡單程式碼示範了「螢幕方向鎖定」、「保持螢幕常亮」、「震動觸覺回饋」的基本流程。
想直接體驗完整互動,請看這個線上範例:
lock()
、request()
、vibrate()
綁在 click
之類事件內。requestFullscreen()
,失敗就降級。👉 歡迎追蹤這個系列,我會從 Canvas 開始,一步步帶你認識更多 Web API 🎯