开发者

Determine initial mouse position in JavaScript?

开发者 https://www.devze.com 2023-01-04 12:19 出处:网络
How can I determine the initial mouse position? right now I use onmousemove, but that sometimes only fires when the mouse is actually moved. same with onmouseover.

How can I determine the initial mouse position? right now I use onmousemove, but that sometimes only fires when the mouse is actually moved. same with onmouseover.

If I don't move the mouse, I observed the following开发者_开发问答:

It seems like in IE a mousemove event is always fired right at the beginning, in firefox never, and in chrome strange enough if the script is loaded from an online location, but not if it's loaded from my local disk.

The question arose because I want stuff to dangle on the cursor and I want it to hang there right from the start and not suddenly appear/jump after the first interaction.

If the solution will be browser specific, my target is chrome.

In case someone wants to explain to me the strange mousemove behavior, this is my test:

<html><head>

<script type='text/javascript'> 
window.onload = function(){
    setInterval("document.getElementById('div0').innerHTML = MousePosition.x+' '+MousePosition.y;", 200)
}
</script> 
<script type="text/javascript" src="http://scripterlative.com/files/mousepos.j_s"></script> 
<!--<script type="text/javascript" src="mousepos.j_s"></script>--> 

</head><body>   

<div id="div0"></div>

<script type='text/javascript'>
MousePosition.init();
</script>

</body></html>

PS: you do not need to lecture me about the use of innerHTML and onload, this is quick and dirty on purpose.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号