本文介绍了javascript onmousemove获取相对坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function aim() {
    var mousex = ?
}

<div class="gameArea" onmousemove="aim();"> </div>

我找不到如何在javascript中获取onmousemove事件的鼠标坐标。

I can't find how to get the mouse coordinates for an onmousemove event in javascript.

推荐答案

var guide = null;

function aim(event) {
    if(window.event)
        event = window.event; //grrr IE
    var mousex = event.clientX - guide.offsetLeft;
    alert('mouse x' + mousex)
}
function init() {
    guide = document.getElementById("guide")
    guide.onmousemove = aim;
}

<body onload="init();">

这似乎在两台浏览器上都有效。我不能做onmousemove =aim();在html中,因为它没有通过mousemove事件对象。

This appears to work on both browsers. I cant do onmousemove="aim();" in html because it doesn't pass the mousemove event object.

这篇关于javascript onmousemove获取相对坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 12:41