当前位置:K88软件开发文章中心编程语言APP编程Cordova → 文章内容

Cordova 振动

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:49:22

由 bjcl 创建,youj 最后一次修改 2016-12-26 此插件用于连接设备的振动功能。步骤1 - 安装Vibration插件我们可以通过运行以下代码在命令提示符窗口中安装此插件:C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-vibration步骤2 - 添加按钮安装插件后,我们可以在 index.html 中添加按钮,稍后将用于触发振动。<button id = "vibration">VIBRATION</button><button id = "vibrationPattern">PATTERN</button>步骤3 - 添加事件监听器现在我们将在 index.js 中的 onDeviceReady 内添加事件监听器。document.getElementById("vibration").addEventListener("click", vibration);document.getElementById("vibrationPattern").addEventListener("click", vibrationPattern);步骤4 - 创建函数这是插件非常容易使用。我们将创建两个函数。function vibration() { var time = 3000; navigator.vibrate(time);}function vibrationPattern() { var pattern = [1000, 1000, 1000, 1000]; navigator.vibrate(pattern);}第一个功能是使用时间参数。此参数用于设置振动的持续时间。一旦按下 VIBRATION 按钮,设备将振动三秒钟。第二个函数是使用 pattern 参数。此阵列将要求设备振动一秒钟,然后等待一秒钟,然后重复该过程。

Cordova 振动