振动 API

Most modern mobile devices include vibration hardware, which lets software code provide physical feedback to the user by causing the device to shake. The 振动 API offers Web apps the ability to access this hardware, if it exists, and does nothing if the device doesn't support it.

Describing vibrations

Vibration is described as a pattern of on-off pulses, which may be of varying lengths. The pattern may consist of either a single integer, describing the number of milliseconds to vibrate, or an array of integers describing a pattern of vibrations and pauses. Vibration is controlled with a single method: Navigator.vibrate() .

A single vibration

You may pulse the vibration hardware one time by specifying either a single value or an array consisting of only one value:

window.navigator.vibrate(200);
window.navigator.vibrate([200]);
					

Both of these examples vibrate the device for 200 ms.

Vibration patterns

An array of values describes alternating periods in which the device is vibrating and not vibrating. Each value in the array is converted to an integer, then interpreted alternately as the number of milliseconds the device should vibrate and the number of milliseconds it should not be vibrating. For example:

window.navigator.vibrate([200, 100, 200]);
					

This vibrates the device for 200 ms, then pauses for 100 ms before vibrating the device again for another 200 ms.

You may specify as many vibration/pause pairs as you like, and you may provide either an even or odd number of entries; it's worth noting that you don't have to provide a pause as your last entry since the vibration automatically stops at the end of each vibration period.

Canceling existing vibrations

调用 Navigator.vibrate() 采用值 0 , an empty array, or an array containing all zeros will cancel any currently ongoing vibration pattern.

Continued vibrations

Some basic setInterval and clearInterval action will allow you to create persistent vibration:

var vibrateInterval;
// Starts vibration at passed in level
function startVibrate(duration) {
    navigator.vibrate(duration);
}
// Stops vibration
function stopVibrate() {
    // Clear interval and stop persistent vibrating
    if(vibrateInterval) clearInterval(vibrateInterval);
    navigator.vibrate(0);
}
// Start persistent vibration at given duration and interval
// Assumes a number value is given
function startPersistentVibrate(duration, interval) {
    vibrateInterval = setInterval(function() {
        startVibrate(duration);
    }, interval);
}
					

Of course, the snippet above doesn't take into account the array method of vibration; persistent array-based vibration will require calculating the sum of the array items and creating an interval based on that number (with an additional delay, probably).

规范

规范 状态 Comment
振动 API Recommendation Linked to spec is the latest editor's draft; W3C version is a REC.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
vibrate Chrome 32 Edge ≤79 Firefox 16
16
Until Firefox 26 included, when the vibration pattern was too long or any of its elements too large, Firefox threw an exception instead of returning false ( bug 884935 ).
From Firefox 32 onwards, when the vibration pattern is too long or any of its elements too large, it returns true but truncates the pattern ( bug 1014581 ).
Beginning in Firefox 72, this is not supported in cross-origin iframes.
11
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE No Opera No Safari No WebView Android 4.4.3
4.4.3
Beginning in version 55, this is not supported in cross-origin iframes.
Beginning in version 60, this method requires a user gesture. Otherwise it returns false .
Chrome Android 32
32
Beginning in Chrome 55, this is not supported in cross-origin iframes.
Beginning in Chrome 60, this method requires a user gesture. Otherwise it returns false .
Firefox Android 16
16
Until Firefox 26 included, when the vibration pattern was too long or any of its elements too large, Firefox threw an exception instead of returning false ( bug 884935 ).
From Firefox 32 onwards, when the vibration pattern is too long or any of its elements too large, it returns true but truncates the pattern ( bug 1014581 ).
14
Prefixed
Prefixed Implemented with the vendor prefix: moz
Opera Android Yes
Yes
Beginning in Opera 47, this method requires a user gesture. Otherwise it returns false .
Safari iOS No Samsung Internet Android 2.0
2.0
Beginning in Samsung Internet 6.0, this is not supported in cross-origin iframes.
Beginning in Samsung Internet 8.0, this method requires a user gesture. Otherwise it returns false .

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅