码迷,mamicode.com
首页 > 其他好文 > 详细

事件基础

时间:2014-09-15 00:54:47      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   java   ar   for   

<!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>

  

事件基础

标签:style   blog   http   color   io   os   java   ar   for   

原文地址:http://www.cnblogs.com/alphafly/p/3971973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!