点评:摇一摇换颜色的,非常好用,不过测试后,需要有很多的地方需要注意,下面是个人的一些见解,大家不妨参考下
我从网上找到了一个案例,是用来摇一摇换颜色的,非常好用,不过测试后,需要注意的有:复制代码
代码如下:
<script type="text/javascript">
var color = new Array('red', 'blue', 'yellow', 'green', '#00f', '#0ff');
if(window.DeviceMotionEvent) {
var speed = 25;
var x = y = z = lastX = lastY = lastZ = 0;
window.addEventListener('devicemotion', function(){
var acceleration =event.accelerationIncludingGravity;
x = acceleration.x;
y = acceleration.y;
//z = acceleration.z;
if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed ) {
document.body.style.backgroundColor = color[Math.round(Math.random()*10)%6];
}
lastX = x;
lastY = y;
//lastZ = z;
}, false);
}
</script>
新闻热点
疑难解答