js 獲取坐標 通過JS得到當前焦點(鼠標)的坐標屬性
更新時間:2013年01月04日 10:35:56 作者:
通過JS得到當前焦點的坐標,如何實現(xiàn),接下來詳細介紹實現(xiàn)步驟,有需要的朋友可以參考下
通過JS得到當前焦點的坐標
如下是得到當前焦點的坐標:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<meta name="auther" content="fq" />
<title>獲取鼠標坐標</title>
</head>
<body>
<script type="text/javascript">
function mousePosition(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mousePosition(ev);
document.getElementById('xxx').value = mousePos.x;
document.getElementById('yyy').value = mousePos.y;
}
document.onmousemove = mouseMove;
</script>
X:<input id="xxx" type="text" /> Y:<input id="yyy" type="text" />
</body>
</html>
如下是得到當前焦點的坐標:
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<meta name="auther" content="fq" />
<title>獲取鼠標坐標</title>
</head>
<body>
<script type="text/javascript">
function mousePosition(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mousePosition(ev);
document.getElementById('xxx').value = mousePos.x;
document.getElementById('yyy').value = mousePos.y;
}
document.onmousemove = mouseMove;
</script>
X:<input id="xxx" type="text" /> Y:<input id="yyy" type="text" />
</body>
</html>
相關文章
Javascript類定義語法,私有成員、受保護成員、靜態(tài)成員等介紹
JS只是一門支持面向對象編程的語言,通過OO可以讓我們的代碼組織更加人性化。可是與傳統(tǒng)基與類的面向對編程語言不同它沒有類概念并且沒成員訪問修飾符。這多少會給我們編程工作會帶來一些束縛2011-12-12filters.revealTrans.Transition使用方法小結
有看到幻燈片調用會用到divid.filters.revealTrans.Transition=Math.floor(Math.random()*23)和divid.filters.revealTrans.apply(),那么這兩個都是什么意思呢?2010-08-08js鼠標經(jīng)過tab選項卡時實現(xiàn)切換延遲
這篇文章主要為大家詳細介紹了js鼠標經(jīng)過tab選項卡時實現(xiàn)切換延遲效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03使用JSX 建立 Markup 組件風格開發(fā)的示例(前端組件化)
這篇文章主要介紹了使用JSX 建立 Markup 組件風格開發(fā)的示例(前端組件化)本文重點講解如何從0搭建一個組件系統(tǒng),在這里我們會學習使用JSX來建立markup 的風格,我們基于與React 一樣的 JSX 去建立我們組件的風格2021-04-04