模拟贪吃蛇

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        window.onload = function () {
            var aDiv = document.getElementsByTagName("div");
            document.onmousemove = function (ev) {
                var oEvent = ev || event;
                for (var i = aDiv.length - 1; i > 0; i--) {
                    aDiv[i].style.left = aDiv[i - 1].offsetLeft + 'px';
                    aDiv[i].style.top = aDiv[i - 1].offsetTop + 'px';
                }
                aDiv[0].style.left = oEvent.clientX + "px";
                aDiv[0].style.top = oEvent.clientY + "px";
            }
        }
    </script>
    <style>
        div {
            width: 20px;
            height: 20px;
            background: red;
            position: absolute;
        }
    </style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>

 

posted @ 2017-05-18 16:02  曦言鸢默  阅读(322)  评论(0编辑  收藏  举报