<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>摇一摇功能</title>
<style>
body{background:#000;}
.hand { width: 190px; height: 300px; background: url(http://www.w3cfuns.com/data/attachment/album/201501/08/105656rplil892bx2l33r9.png) no-repeat;
margin:40px auto; }
.hand-animate { -webkit-animation: hand_move infinite 2s; }
@-webkit-keyframes hand_move {
0% {
-webkit-transform: rotate(0);
-moz-transform: rotate(0);
-ms-transform: rotate(0);
-o-transform: rotate(0);
transform: rotate(0); }
50% {
-webkit-transform: rotate(15deg);
-moz-transform: rotate(15deg);
-ms-transform: rotate(15deg);
-o-transform: rotate(15deg);
transform: rotate(15deg); }
100% {
-webkit-transform: rotate(0);
-moz-transform: rotate(0);
-ms-transform: rotate(0);
-o-transform: rotate(0);
transform: rotate(0); }
}
.hand_one{margin:0 auto;color:#fff;font-size:24px;display:block;width:200px;}
.fall{margin:0 auto;color:#f99;font-size:24px;display:block;width:220px;}
.loading { position: absolute; top: 270px; left: 50%; margin-left: -50px; width: 100px; height: 100px;
background: url(http://www.w3cfuns.com/data/attachment/album/201501/08/105657t5bk4va85qnci55v.png) no-repeat; background-size: 100px 100px; opacity: 0;
-webkit-animation: loading infinite linear .5s;
-moz-animation: loading infinite linear .5s;
-ms-animation: loading infinite linear .5s;
-o-animation: loading infinite linear .5s;
animation: loading infinite linear .5s;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-ms-transition: all .5s;
-o-transition: all .5s;
transition: all .5s; }
.loading-show { opacity: 1; }
@-webkit-keyframes loading {
0% {
-webkit-transform: rotate(0);
-moz-transform: rotate(0);
-ms-transform: rotate(0);
-o-transform: rotate(0);
transform: rotate(0); }
100% {
-webkit-transform: rotate(360deg);
-moz-transform: rotate(360deg);
-ms-transform: rotate(360deg);
-o-transform: rotate(360deg);
transform: rotate(360deg); }
}
</style>
</head>
<body οnlοad="init()">
<p class="hand_one">喵~试试手气呦!</p>
<div id="hand" class="hand hand-animate"></div>
<div id="loading" class="loading"></div>
<div id="fall" class="fall"></div>
</body>
</html>
<script>
var SHAKE_THRESHOLD = 400;
var last_update = 0;
var index=0;
var x = y = z = last_x = last_y = last_z = 0;
var w_curTime=0;
function init() {
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
alert('not support mobile event');
}
}
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;
if (speed > SHAKE_THRESHOLD) {
if((curTime-w_curTime)>2000){
w_curTime!=0 && doResult();
w_curTime=curTime;
}
}
last_x = x;
last_y = y;
last_z = z;
}
}
function doResult() {
document.getElementById("fall").innerHTML="";
document.getElementById("loading").className = "loading loading-show";
setTimeout(function(){
index=parseInt(Math.random()*2);//parseInt(Math.random()*(上限-下限+1)+下限);
if(index==0){
document.getElementById("fall").innerHTML="恭喜您,中奖了!!";
}else{
document.getElementById("fall").innerHTML="再试试呦!!";
}
document.getElementById("loading").className = "loading";
}, 1000);
}
</script>
html5 摇一摇
最新推荐文章于 2021-06-23 06:46:28 发布