clientX,offsetX,layerX,pageX,screenX,X鼠标位置全解

下面是w3c关于各属性的解释:

 

  pageX/Y被划到了jq里:

 

  没找到layerX/Y的官方文档。

 

  总结:

  推荐使用:

  screenX/Y:鼠标位置相对于屏幕的坐标

  pageX/Y:相对于文档边缘(包含滚动条距离)

  clientX/Y:相对于当前页面且不包含滚动条距离

  offsetX/Y:相对于当前元素(块或行内块),除safari外不包含边框。

  其他:

  X/Y:与clientX/Y相同,firefox不支持

  layerX/Y:除IE外与pageX/Y相同,IE11下与clientX/Y相同。非官方属性。

 

  测试代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        .div {
            text-align: center;
            font-size: 24px;
            height: 300px;
            width: 1300px;
            line-height: 300px;
            color: yellow;
        }
 
        #d1 {
            background-color: #FF3B2F;
        }
 
        #d2 {
            background-color: #4CDA64;
        }
 
        #d3 {
            background-color: #007AFF;
            border: 50px solid #004400;
            width: 500px;
            display: inline-block; }
        #d3-2{
            background-color: #FF2C55;
            width: 500px;
            border: 10px solid #019EE4;
            display: inline-block;
        }
 
        #d4 {
            position: absolute;
            background-color: #FFCC00;
            height: 340px;
            width: 120px;
            top: 0;
            bottom: 0;
            left: 50px;
            margin: auto 0;
            font-family: "arial";
            font-size: 16px;
        }
    </style>
    <script type="text/javascript">
 
        $(function () {
 
            document.onmousemove = function (e) {
                if (e == null) {
                    e = window.event;
                }  var html = "<span style='color:#000'>screenX:" + e.screenX + "</span><br/>";
                html += "<span style='color:#000'>screenY:" + e.screenY + "</span><br/><br/>";
                 
                html += "<span style='color:#f00'>clientX:" + e.clientX + "</span><br/>";
                html += "<span style='color:#f00'>clientY:" + e.clientY + "</span><br/><br/>";
                html += "<span style='color:#f00'>x:" + e.x + "</span><br/>";
                html += "<span style='color:#f00'>y:" + e.y + "</span><br/><br/>";
                 
                html += "<span style='color:#00f'>layerX:" + e.layerX + "</span><br/>";
                html += "<span style='color:#00f'>layerY:" + e.layerY + "</span><br/><br/>";
                html += "<span style='color:#00f'>pageX:" + e.pageX + "</span><br/>";
                html += "<span style='color:#00f'>pageY:" + e.pageY + "</span><br/><br/>";
                 
                html += "<span style='color:#070'>offsetX:" + e.offsetX + "</span><br/>";
                html += "<span style='color:#070'>offsetY:" + e.offsetY + "</span><br/>";
                $("#d4").html(html);
            };
        });
 
    </script>
</head>
<body>
    <div id="d1" class="div">div1 height:300px width:1300px</div>
    <div id="d2" class="div">div2 height:300px width:1300px</div>
    <div id="d3" class="div">div3 height:300px width:500px</div>
    <div id="d3-2" class="div">div3-2 height:300px width:500px <span style="width:50px;height:50px;background:#000;display: inline-block;border: 5px solid #fff;line-height: 45px;">dddd</span></div>
    <div id="d4"></div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值