我取這個(gè)標(biāo)題為"JS和jquery獲取當(dāng)前鼠標(biāo)的x、y坐標(biāo)位置誰更簡單?"并不是為了華眾取寵,而是為了說它們有不同的應(yīng)用常合,或者說有些時(shí)候你又為了效率而不得不用JS版的來取得鼠標(biāo)坐標(biāo)的方法
比如Ecshop模板制作中,它本身的JS文件與Jquery有沖突
style
jquery中獲取當(dāng)前鼠標(biāo)的x、y位置位置的代碼,需要的朋友可以參考下。
<div id="testDiv">放在我上面</div>
<script type="text/javascript">
$('#testDiv').mousemove(function(e) {
var xx = e.originalEvent.x || e.originalEvent.layerX || 0;
var yy = e.originalEvent.y || e.originalEvent.layerY || 0;
$(this).text(xx + '---' + yy);
});
</script>
或者
<script type="text/javascript">
$('#testDiv').mousemove(function(e) {
var xx=e.pageX;
var yy=e.pageY;
$(this).text(xx + '---' + yy);
});
</script>
javascript獲取鼠標(biāo)當(dāng)前位置坐標(biāo)
鼠標(biāo)滑動(dòng)顯示鼠標(biāo)的當(dāng)前位置坐標(biāo),主要是onmousemove函數(shù)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript獲得鼠標(biāo)位置</title>
</head>
<body>
<script>
function mouseMove(ev)
{
Ev= ev || window.event;
var mousePos = mouseCoords(ev);
document.getElementByIdx_x_x_xx_x("xxx").value = mousePos.x;
document.getElementByIdx_x_x_xx_x("yyy").value = mousePos.y;
}
function mouseCoords(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
};
}
document.onmousemove = mouseMove;
</script>
鼠標(biāo)X軸:
<input id=xxx type=text>
鼠標(biāo)Y軸:
<input id=yyy type=text>
</body>