ios 震动api

Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in a pattern for a given duration.

浏览器供应商提供给我们的许多新API都比台式机用户更面向移动用户。 这些简单的API之一就是Vibration API 。 振动API允许开发人员使用JavaScript引导设备在给定的时间内以某种模式振动。



View Demo 观看演示




(Detecting Vibration API Support)

It's always good to check for the presence of API support before using it;  here's how you can detect the Vibration API's presence:

使用API之前最好先检查一下是否存在API支持。 这是检测振动API是否存在的方法:

// Standards ftw!
var supportsVibrate = "vibrate" in navigator;

The Vibration API consists of only one method provided to the window.navigator object:  vibrate.

Vibration API仅包含提供给window.navigator对象的一种方法: vibrate

(Vibration API Basics)

The navigator.vibrate function accepts either a single number or an array of numbers for a series of vibrations. When using the array method, the even indices represent vibration duration, the odd indices represent a delay before the next vibration.

navigator.vibrate函数接受一系列振动的单个数字或数字数组。 当使用阵列方法时,偶数索引表示振动持续时间,奇数索引表示下一次振动之前的延迟。

// Vibrate once for one second
navigator.vibrate(1000);

// Vibrate multiple times for multiple durations
// Vibrate for three seconds, wait two seconds, then vibrate for one second
navigator.vibrate([3000, 2000, 1000]);

To stop vibration when active, simply pass a 0 or an empty array to the navigator.vibrate method:

要在激活时停止振动,只需将0或空数组传递给navigator.vibrate方法:

// Either of these stop vibration
navigator.vibrate(0);
navigator.vibrate([]);

Realize that vibrations do not loop until stopped with 0 or an empty array;  the single number vibration occurs once and then becomes silent, the array of vibration durations run and becomes silent again.

意识到直到用0或空数组停止振动循环。 单个数振动发生一次然后变为静音,振动持续时间阵列运行并再次变为静音。

(Continued Vibration)

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

一些基本的setIntervalclearInterval动作将使您创建持久的振动:

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 startPeristentVibrate(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).

当然,上面的代码片段并未考虑振动的阵列方法; 基于数组的持久振动将需要计算数组项的总和,并基于该数量创建一个间隔(可能会有额外的延迟)。



View Demo 观看演示




(Why Use the Vibration API?)

This API is clearly targeted toward mobile devices.  The Vibration API would be good for alerts within mobile web applications, and would be especially awesome when used in games or media-heavy applications.  Imagine watching a video on your mobile device, and during an explosion scene, your phone got a bit of a shake.  Or playing Bomberman and feeling a gentle kick when a block explodes!

该API显然是针对移动设备的。 振动API非常适合在移动Web应用程序中发出警报,并且在用于游戏或大量媒体应用程序中时特别棒。 想象一下在移动设备上观看视频,在爆炸场景中,您的手机有些震动。 或扮演Bomberman并在方块爆炸时感到柔和的踢脚!

What do you think of the Vibration API:  immediately useful or not quite yet?

您如何看待Vibration API:立即有用还是不太有用?

At the time of writing, Firefox BETA for Android is the only browser which supports the Vibration API. WebKit landed the Vibration API a while back, but in my testing of iOS Chrome and Safari, as well as Android Chrome and standard browser, I could not find a working vibration property. Opera doesn't appear to support vibration yet either.

在撰写本文时,适用于Android的Firefox BETA是唯一支持Vibration API的浏览器。 WebKit不久前投放了Vibration API ,但在我对iOS Chrome和Safari以及Android Chrome和标准浏览器的测试中,我找不到有效的振动属性。 Opera似乎也不支持振动。





翻译自: https://davidwalsh.name/vibration-api

ios 震动api