`
cuiyadll
  • 浏览: 196254 次
文章分类
社区版块
存档分类
最新评论

分享一个有趣的js代码(手机摇一摇切换网页背景色)

    博客分类:
  • js
 
阅读更多
引用
<script type="text/javascript">
var color = new Array('#fff', '#ff0', '#f00', '#000', '#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;
        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;
    }, false);
}
</script>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics