js擷取滑鼠位置

來源:互聯網
上載者:User

標籤:

1.PageX/PageX:滑鼠在頁面上的位置,從頁面左上方開始,即是以頁面為參考點,不隨滑動條移動而變化
2.clientX/clientY:滑鼠在頁面上可視地區的位置,從瀏覽器可視地區左上方開始,即是以瀏覽器滑動條此刻的滑動到的位置為參考點,隨滑動條移動 而變化.
可是悲劇的是,PageX只有FF特有,IE這個悲劇沒有啊T_T,所以大牛們想出了一個辦法
PageY=clientY+scrollTop-clientTop;(只討論Y軸,X軸同理,下同)
3.screenX/screenY:滑鼠在螢幕上的位置,從螢幕左上方開始(w3c標準)
4.offsetX/offsetY:IE特有,滑鼠相比較於觸發事件的元素的位置,以元素盒子模型的內容地區的左上方為參考點,如果有boder,可能出現負值
5.
layerX/layerY:FF特有,滑鼠相比較於當前座標系的位置,即如果觸發元素沒有設定絕對位置或相對定位,以頁面為參考點,如果有,將改變參考座標系,從觸發元素盒子模型的border地區的左上方為參考點也就是當觸發元素設定了相對或者絕對位置後,layerX和offsetX就幸福地生活在一起^-^,幾乎相等,唯一不同就是一個從border為參考點,一個以內容為參考點

chrome和safari一條龍通殺!完全支援所有屬性.其中(offsetX和layerX都是以border為參考點)

下面這個是擷取相對於螢幕的座標
document.onmousemove=function(e){e=e? e:window.event;document.writeln("X:"+e.screenX+"Y:"+e.screenY);}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_0922w.WebForm1" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title>    <script type="text/javascript">        function getScreen(event) {            document.getElementById("container").innerHTML = event.offsetX + " , " + event.offsetX;        }    </script>    <style>        #container {                left:0px;        bottom:0px;        text-align:left;        width:293px;        height:190px;        }    </style>        </head><body>    <form id="form1" runat="server">    <div onmousemove="getScreen(event)" id="container">        <asp:Image ID="Image1" runat="server" ImageUrl="~/image/1152165471 - 副本.jpg" />            </div>        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    </form></body></html>

 

js擷取滑鼠位置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.