熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JS獲取鼠標坐標的實例方法

2022-06-13   來源: JSP教程 
這篇文章介紹了JS獲取鼠標坐標的實例方法有需要的朋友可以參考一下   復制代碼 代碼如下:
var restrictX; 
var restrictY; 
var tip; 
// 鼠標坐標  
function mousePosition(ev) { 
return { 
        x : evclientX + documentdocumentElementscrollLeft documentdocumentElementclientLeft 
        y : evclientY + documentdocumentElementscrollTop documentdocumentElementclientTop 
    }; 

// 鼠標事件  
function mouseMove(ev) { 
    ev = ev || windowevent; 
    var mousePos = mousePosition(ev); 
    restrictX = mousePosx; 
    restrictY = mousePosy; 

documentonmousemove = mouseMove; 
documentonclick = mouseMove; 

  
上面的代碼在谷歌和搜狐浏覽器中獲取的值會不准確需進行修改如下

復制代碼 代碼如下:
var restrictX; 
var restrictY; 
var tip; 
// 鼠標坐標  
function mousePosition(ev){ 
    var scrollLeft = documentdocumentElementscrollLeft || documentbodyscrollLeft; 
    var scrollTop = documentdocumentElementscrollTop || documentbodyscrollTop; 
    return { 
        x:evclientX + scrollLeft documentdocumentElementclientLeft 
        y:evclientY + scrollTop documentdocumentElementclientTop 
    }; 

// 鼠標事件  
function mouseMove(ev){ 
    ev = ev || windowevent; 
    var mousePos = mousePosition(ev); 
    restrictX = mousePosx; 
    restrictY = mousePosy; 

documentonmousemove = mouseMove; 
documentonclick = mouseMove;  復制代碼 代碼如下:
var scrollLeft = documentdocumentElementscrollLeft || documentbodyscrollLeft;
var scrollTop = documentdocumentElementscrollTop || documentbodyscrollTop; 

  
這兩句為如果能獲取到鼠標坐標則去前面否則用後面的方法獲取鼠標坐標“|| ”後面的是給WebKit 內核的浏覽器使用


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20065.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.