<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<title> HTML5 重力感应效果,实现摇一摇效果 </title>
<style>
html,body,div,ul,li{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
body{
width:100%;
}
#box{
width:90%;
height:80px;
background-color:orange;
margin:0 auto;
color:#fff;
background-position: center -50px;
}
</style>
</head>
<body> <div id="box"></div> <script>
//摇晃的力度
var SHAKE_THRESHOLD = 2000;
var last_update = 0;
//初始化重力感应位置
var x = y = z = last_x = last_y = last_z = 0; function deviceMotionHandler(eventData) {
var acceleration = eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();
if ( (curTime - last_update) > 100 ) {
var diffTime = curTime - last_update;
last_update = curTime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
last_x = x;
last_y = y;
last_z = z; //获取 X Y Z 的数值
document.getElementById('box').innerHTML = 'X:'+last_x+'<br> Y:'+last_y+'<br> Z:'+last_z; //如果速度大于摇晃的力度那么执行alert
if (speed > SHAKE_THRESHOLD) {
alert('摇一摇');
}
}
}
//判断是否支持重力感应
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
alert('not support mobile event');
} </script>
</body>
</html>

  

HTML5 重力感应效果,实现摇一摇效果的更多相关文章

  1. H5案例分享:html5重力感应事件

    html5重力感应事件 一.手机重力感应图形分析 1.设备围绕z轴的旋转角度为α,α角度的取值范围在[0,360). 设备在初始位置,与地球(XYZ)和身体(XYZ)某个位置对齐. 设备围绕z轴的旋转 ...

  2. html5重力感应事件之DeviceMotionEvent

    前言 今天主要介绍一下html5重力感应事件之DeviceMotionEvent,之前我的一篇文章http://www.haorooms.com/post/jquery_jGestures, 介绍了第 ...

  3. HTML5重力感应小球冲撞动画实现教程

    今天我们来分享一款很酷的HTML5重力感应动画教程,这款动画可以让你甩动页面中的小球,小球的大小都不同,并且鼠标点击空白区域时又可以生成一定数量的小球.当我们甩动小球时,各个小球之间就会发生互相碰撞的 ...

  4. h5手机摇一摇功能实现:基于html5重力感应DeviceMotionEvent事件监听手机摇晃

    DeviceMotionEven是html5提供的一个用来获取设备物理方向及运动的信息(比如陀螺仪.罗盘及加速计)的Dom事件,事件描述如下: deviceorientation:提供设备的物理方向信 ...

  5. 移动端html5重力感应

    下面是测试案例,只测试过itouch,iphone http://06wjin.sinaapp.com/billd/     http://06wjin.sinaapp.com/billd/test. ...

  6. html5重力感应事件

    if (window.DeviceMotionEvent) { window.addEventListener('devicemotion',deviceMotionHandler, false); ...

  7. 【HTML5 】手机重力与方向感应的应用——摇一摇效果

    http://www.helloweba.com/view-blog-287.html HTML5有一个重要特性:DeviceOrientation,它将底层的方向和运动传感器进行了高级封装,它使我们 ...

  8. HTML5实现“摇一摇”效果

    在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...

  9. 手机摇一摇效果-html5

    1.手机摇一摇效果实现 2.播放声音 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

随机推荐

  1. UNDO表空间不足解决方法

    确认UNDO表空间名称 select name from v$tablespace; 检查数据库UNDO表空间占用空间情况以及数据文件存放位置: select file_name,bytes/1024 ...

  2. 要自己当技术使用astgo运营网络电话系统,必须掌握的基本技术

    知道什么是centos 知道怎么远程访问centos服务器 (常用工具 Secure Shell Client.WINSCP) 知道重启服务器的命令是 reboot 知道你的服务器是没有图形界面的,所 ...

  3. Java开源JSP标签库

    01displytag 与Struts结合使用最出名的一个tag主要是显示表格数据很漂亮.完善. 02cewolf tag 用来在web上显示复杂图形报表的一个jsp tag. 03Loading T ...

  4. ASP.NET MVC5 之路由器

    这篇博客介绍的很详细 http://www.cnblogs.com/yaozhenfa/p/asp_net_mvc_route_1.html

  5. hdu2030

    http://acm.hdu.edu.cn/showproblem.php?pid=2030 #include<stdio.h> #include<math.h> #inclu ...

  6. html5——伸缩比例案例(携程)

    1.有图片的盒子,最好是父盒子设置伸缩属性,a标签设置伸缩比例1,img标签宽度100% 2.不要见到父盒子就设置伸缩属性,而是根据子盒子是否占据一行,若是子盒子占据一行,那么只要给子盒子设置伸缩比例 ...

  7. CSS——◇demo

    核心思想:嵌套盒子中的◇超过父盒子的部分隐藏. 第一种写法: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  8. hdu,1028,整数拆分的理解

    #include"iostream"using namespace std;int main() { int n,i,j,k; int c[122],temp[122]; //c[ ...

  9. python 将中文转拼音后填充到url做参数并写入excel

    闲着没事写了个小工具,将中文转拼音后填充到url做参数并写如excel 一.先看下演示,是个什么东西 二.代码 代码用到一个中文转拼音的库,库是网上下的,稍微做了下修改,已经找不原来下载的地址了,然后 ...

  10. Power Designer逆向操作(从mysql5.0生成数据库的物理模型)

    Power Designer逆向操作(从mysql5.0生成数据库的物理模型) 环境:powderdesigner12.5:mysql5.0 步骤: 1.  为指定的数据库配置MySQL的ODBC数据 ...