本文档部分API缺少示例代码,诚挚的邀请你参与到示例代码的编写中来,你可以在代码中发布2行自定义注释,内容自拟。 有意者可与刺鸟联系(QQ:4041990 Q群:7702175)
常用代码 » appcan实现摇一摇功能
<!DOCTYPE html>
<html>
<head>
<style>body{ background:#fff; font-size:30px;}</style>
<meta charset="utf-8">
<script>
//摇一摇
var shake = {
bind : function(callback,level,delay){
var me = this;
if(!window.uexSensor)return;
uexSensor.open(1,3);
me.callback = callback;
me.lastCall = new Date();
me.level = level || 4; //灵敏等级
me.delay = delay || 2000; //2000ms内只触发一次
uexSensor.onAccelerometerChange = me.orientation;
alert('绑定完成..');
},
orientation : function(x,y,z){
var me = shake;
if(!me.lastY){me.lastY=y}
var result = Math.abs(y - me.lastY);
me.lastY=y;
if(result*1>me.level && new Date()-me.lastCall>me.delay){
me.lastCall = new Date();
uexDevice.vibrate(100);
me.callback && me.callback();
}
},
close : function(){
window.uexSensor && window.uexSensor.close(1);
}
};
window.uexOnload = function(){
//开启摇一摇功能,绑定摇晃后的事件
shake.bind(function(){
alert('摇啥呢...');
});
uexWidget.onSuspend = function(){
//程序锁屏时,关闭摇一摇监测
shake.close();
};
};
</script>
</head>
<body>
<p>摇一摇设备试试....</p>
</body>
</html>