HTML5 重力感应效果,实现摇一摇效果
<!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 重力感应效果,实现摇一摇效果的更多相关文章
- H5案例分享:html5重力感应事件
html5重力感应事件 一.手机重力感应图形分析 1.设备围绕z轴的旋转角度为α,α角度的取值范围在[0,360). 设备在初始位置,与地球(XYZ)和身体(XYZ)某个位置对齐. 设备围绕z轴的旋转 ...
- html5重力感应事件之DeviceMotionEvent
前言 今天主要介绍一下html5重力感应事件之DeviceMotionEvent,之前我的一篇文章http://www.haorooms.com/post/jquery_jGestures, 介绍了第 ...
- HTML5重力感应小球冲撞动画实现教程
今天我们来分享一款很酷的HTML5重力感应动画教程,这款动画可以让你甩动页面中的小球,小球的大小都不同,并且鼠标点击空白区域时又可以生成一定数量的小球.当我们甩动小球时,各个小球之间就会发生互相碰撞的 ...
- h5手机摇一摇功能实现:基于html5重力感应DeviceMotionEvent事件监听手机摇晃
DeviceMotionEven是html5提供的一个用来获取设备物理方向及运动的信息(比如陀螺仪.罗盘及加速计)的Dom事件,事件描述如下: deviceorientation:提供设备的物理方向信 ...
- 移动端html5重力感应
下面是测试案例,只测试过itouch,iphone http://06wjin.sinaapp.com/billd/ http://06wjin.sinaapp.com/billd/test. ...
- html5重力感应事件
if (window.DeviceMotionEvent) { window.addEventListener('devicemotion',deviceMotionHandler, false); ...
- 【HTML5 】手机重力与方向感应的应用——摇一摇效果
http://www.helloweba.com/view-blog-287.html HTML5有一个重要特性:DeviceOrientation,它将底层的方向和运动传感器进行了高级封装,它使我们 ...
- HTML5实现“摇一摇”效果
在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...
- 手机摇一摇效果-html5
1.手机摇一摇效果实现 2.播放声音 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
随机推荐
- Setup Script in SoapUI - 停止项目运行 (abort project)
TestSuite需要依赖一个先决条件(比如Login) 当Login失败则立即停止Project运行 在Project的Setup Script的代码如下 import com.eviware.so ...
- adb protocol failure【转】
本文转载自:http://blog.csdn.net/hang2/article/details/45080769 今天遇见一个现象 在Nexus4上面部分adb功能失效, 可以 adb push 到 ...
- Jsp内置对象和EL隐藏(内置)对象
JSP中的内置对象一共有九个, 由于有的不太常用, 所以总是记不住, 从Sun公司的网站上找到的PDF文档, 把这一部分放在这里, 以备随时查用: JSP九个内置对象: Implicit ...
- Potted Flower(线段树+dp)
http://poj.org/problem?id=2750 题意:在一个圈中取若干个相邻的数,求他们的最大序列和.不能够同时取所有的数. 看了一篇解题报告写的很详细..http://blog.csd ...
- 自定义滚动条配合鼠标滚轮demo
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...
- Akka源码分析-Actor发消息
前面两篇文章简单介绍了ActorSystem.actor以及dispatcher和mailbox的创建,下面我们就来看一下actor发消息的内部机制. val system = ActorSystem ...
- javascript 处理链接的多种方式
在页面中的链接除了常规的方式以外,如果使用javascript,还有很多种方式,下面是一些使用javascript,打开链接的几种方式: 1.使用window的open方法打开链接,这里可是在制定页面 ...
- 虚拟机下安装VM
Linux(CentOS 7)命令行模式安装VMware Tools 详解 [日期:2017-05-02] 来源:Linux社区 作者:Linux [字体:大 中 小] 本篇文章主要介绍了如何在Li ...
- 题解报告:hdu 1285 确定比赛名次
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...
- 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle
问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...