zoukankan      html  css  js  c++  java
  • 获取鼠标的坐标

    <head runat="server">
        <script type="text/javascript">
            function getX(obj) {
                var parObj = obj;
                var left = obj.offsetLeft;
                while (parObj = parObj.offsetParent) {
                    left += parObj.offsetLeft;
                }
                return left;
            }

            function getY(obj) {
                var parObj = obj;
                var top = obj.offsetTop;
                while (parObj = parObj.offsetParent) {
                    top += parObj.offsetTop;
                }
                return top;
            }

            function DisplayCoord(event) {
                var top, left, oDiv;
                oDiv = document.getElementById("demo");
                top = getY(oDiv);
                left = getX(oDiv);
                document.getElementById("mp_x").innerHTML = (event.clientX - left + document.body.scrollLeft)  + "px";
                document.getElementById("mp_y").innerHTML = (event.clientY - top + document.body.scrollTop) + "px";
            }   
        </script>
    </head>
    <body style="margin: 40px;" mce_style="margin:40px;">
        <div style="background-color: #000000; color: #0011FF; 300px; height: 300px;
            position: absolute; top: 80px; left: 90px; margin: 0px; border: 0px;" id="demo"
            onmousemove="DisplayCoord(event)">
        </div>
        当前鼠标坐标为: X:<span id="mp_x"></span> Y:<span id="mp_y"></span>
    </body>

  • 相关阅读:
    反射学习笔记
    路由机制
    缓存笔记
    进程和线程
    垃圾回收机制
    堆和栈
    值类型和引用类型及参数传递
    招到一个程序员很难吗?
    全面讲解:委托、事件
    struts2入门实例
  • 原文地址:https://www.cnblogs.com/zhanjun/p/2840780.html
Copyright © 2011-2022 走看看