小程序_微信重力感应 —— 摇一摇

使用该功能的页面引用下面这段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// .js
Page({

isShow: false,
onShow: function () { // 页面打开
var that = this;
this.isShow = true;
wx.onAccelerometerChange(function (e) { // 微信重力
if(!that.isShow){
return
}
console.log(e.x)
console.log(e.y)
console.log(e.z)
if (e.x > 1 && e.y > 1) {
wx.showToast({
title: '摇一摇成功',
icon: 'success',
duration: 2000
})
}
})
},
onHide: function(){ // 页面关闭
this.isShow = false;
}

})