首页 技术 正文
技术 2022年11月6日
0 收藏 950 点赞 411 浏览 1995 个字

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

先放代码

<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;
 }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287