看微信摇一摇之后忽然想知道他是怎么写的。于是就在网上查了一些资料,有些是借鉴别人的。大家共同学习啊

先放代码

<body onload="init()">
<p>用力摇一摇你手机</p>
<audio style="display:hiden;width:0px; height:0px;" id="musicBox" preload="metadata" controls src="swiper/music/default.mp3"> <script>
init();
var SHAKE_THRESHOLD = 3000;//定义一个摇动的值
var last_update = 0;//定义一个变量保存上次更新的时间 var x = y = z = last_x = last_y = last_z = 0;//定义xyz记录三个轴的数据以及上一次出发的时间
function init() {
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
alert('not support mobile event');
}
}
function deviceMotionHandler(eventData) {
var meda01=document.getElementById('musicBox');
var acceleration = eventData.accelerationIncludingGravity;//含重力加速度
var curTime = new Date().getTime();//获取当前时间
if ((curTime - last_update) > 100) {//curTime - last_update 是固定时间段
var diffTime = curTime - last_update;
last_update = curTime;
//alert('last_update='+last_update)
x = acceleration.x;
//alert('x='+x)
y = acceleration.y;
//alert('y='+y)
z = acceleration.z;
//alert('z='+z)
var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
//alert('speed='+speed) abs是取绝对值
if (speed > SHAKE_THRESHOLD) {
alert("摇动了+中大奖了,就是不知道有没有声音");
media.setAttribute("src", "swiper/music/default.mp3");
//setAttribute 锁定元素。此方法不能通过document对象调用,只能通过元素节点对象调用他
//例如,你可以把他与getElementByTagName()方法结合起来,去查询每个<p>元素的title属性
//var txt=document.getElementsByTagName('p')
//for(var i=0;i<text.length;i++){
//alert(txt[i].getAttribute('title'))
//}
//
//
//
//
media.load();
media.play();
meda01.play();
}
last_x = x;
last_y = y;
last_z = z;
}
}

html5 的 deviceorientation 他将底层的方向传感器和运动传感器进行了高级封装。提供了dom事件的支持。这个特性包括两种事件:

1.deviceOrientation:封装了方向传感器数据的事件,可以获取手机静止状态下的方向数据,例如手机所处角度、方位、朝向等。

2.deviceMotion:封装了运动传感数据的事件,可以获取手机运行状态下的运动加速度等数据。使用它我们能够很容易的实现重力感应、指南针等有趣的功能

3.DeviceMotionEvent(设备运动事件)返回设备有关于加速度和旋转的相关信息。加速度的数据讲包含3个轴:x,y,z。该事件会返回两个属性,accelerationIncludingGravity(含重力的加速度)和acceleration(加速度)。

 1.【代码】监听运动传感事件

if(window.DeviceMotionEvent){
window.addEventListener('devicemotion',deviceMotionHandler,false);
}
 2.[代码]获取含重力的加速度

function deviceMotionHandler(eventData){
 var acceleration=eventData.accelerationIncludingGravity;
 }

最新文章

  1. 【Android自学日记】五大布局常用属性
  2. [django]利用xlwt实现文件下载功能
  3. Kanzi编程基础2 - Kanzi节点读取和属性设置
  4. NetBeans IDE配置调试
  5. xp/2003开关3389指令
  6. IE6的position:fixed
  7. Web安全--使用Salt + Hash将密码加密后再存储进数据库
  8. firefox和chrome对于favicon.ico关于content-security-policy的不同处理
  9. 在Ajax中将数组转换成字符串(0517-am)
  10. IOS Layer的使用
  11. Content-Disposition的使用和注意事项(转载)
  12. OpenGL研究2.0 计算圆
  13. 后端自动化版本管理,再也不用改URL了!
  14. C语言中file文件指针概念及其操作 (转载)
  15. [欢乐向]JavaScript之如何逼疯你的同事
  16. vuedraggable(vue2.0)组件详解
  17. WPF Button 样式
  18. Archiva 2.2.3 安装运行的时候出现协议版本错误
  19. Android开发(十六)——Android listview onItemClick事件失效的原因
  20. Linux学习之用户与root

热门文章

  1. Sass变量、嵌套
  2. 在Java中兼容Windows和Linux的路径处理
  3. Swift中类的两段式构造(类的构造过程)
  4. jQuery Alert Dialogs (Alert, Confirm, &amp; Prompt代替方案)
  5. ios app开发步骤
  6. 让PHP跑在Mac OS X中
  7. 用PHP删除文件操作unlink
  8. 字符串处理(正则表达式、NSScanner扫描、CoreParse解析器)-备用
  9. Android从相册中获取图片以及路径
  10. 2015必须要看的APP源码