事件基础

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        div
        {
            width: 20px;
            height: 20px;
            background-color: red;
            position: absolute;
        }
    </style>
    <script type="text/javascript">
        function getPosition(ev) {
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            return { x: ev.clientX + scrollLeft, y: ev.clientY + scrollTop }
        }
        window.onload = function () {
            document.onmousemove = function (ev) {
                var arrDiv = document.getElementsByTagName("div");
                var oEvent = ev || event;
                var pos = getPosition(oEvent);
                for (var i = arrDiv.length - 1; i > 0; i--) {
                    arrDiv[i].style.left = arrDiv[i - 1].offsetLeft + "px";
                    arrDiv[i].style.top = arrDiv[i - 1].offsetTop + "px";
                }
                arrDiv[0].style.left = pos.x + "px";
                arrDiv[0].style.top = pos.y + "px";
            }
        }
    </script>
</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>
</body>
</html>

  

posted @ 2014-09-14 23:24  ICupid  阅读(137)  评论(0编辑  收藏  举报