在调用接口页面的<script>部分增加如下配置。
import vibrator from '@system.vibrator'
或
var vibrator = require("@system.vibrator")
描述
触发震动,持续1秒。
OBJECT参数
参数 |
类型 |
是否必填 |
说明 |
|---|---|---|---|
mode(1030+) |
string |
否 |
振动模式,默认为long。
实现时,long的时长定义为1000ms,short的时长定义为35ms。 |
示例代码
vibrator.vibrate({ mode: "short" });
<template>
<!-- Only one root node is allowed in template. -->
<div class="container">
<div class="item-container">
<input onclick="vibratelong" type="button" value="Long vibration" class="btn-blue"/>
</div>
<div class="item-container">
<input onclick="vibrateshort" type="button" value="Short vibration" class="btn-blue"/>
</div>
<div class="item-container" if="{{info}}">
<text class="content">{{info}}</text>
</div>
</div>
</template>
<style lang="sass">
.container {
flex-direction: column;
justify-content: center;
align-items: center;
}
.item-container {
margin-top: 50px;
margin-right: 30px;
margin-left: 30px;
flex-direction: column;
}
.btn-blue {
background-color: #0faeff;
color: #ffffff;
width:400px;
height: 100px;
font-size: 50px;
border-radius:15px;
}
.content {
border: 2px ;
border-radius: 10px;
text-align: center;
}
</style>
<script>
import vibrator from '@system.vibrator';
module.exports = {
data: {
componentData: {},
info:'',
get_success:'',
get_fail:''
},
onInit: function () {
this.$page.setTitleBar({ text: 'vibrator'});
},
vibratelong(){
var that = this;
vibrator.vibrate({mode: 'long'});
that.info = "long vibration is defined as lasting for 1000 ms"
console.log("long vibration is defined as lasting for 1000 ms");
},
vibrateshort(){
var that = this;
vibrator.vibrate({mode: 'short'});
that.info = "short vibration is defined as lasting for 35 ms"
console.log("short vibration is defined as lasting for 35 ms");
}
}
</script>
效果图如下:
