Mouse position on mouseover event

牧云@^-^@ 提交于 2019-12-08 16:45:42

问题


Is it possible to get the exactly mouse position in a mouseouver event of a image? If i use a function that update the mouse position on document mouse move event I can have problems with delay and this kind of thing and wouldn't get the EXACTLY position


回答1:


If you are looking for a simple JS to get the cursor position for a MouseOver event, here is the sample code:

    <!DOCTYPE html>
    <html>
    <head>
    	<script>
    	
    	function getPos(e){
    		x=e.clientX;
    		y=e.clientY;
    		cursor="Your Mouse Position Is : " + x + " and " + y ;
    		document.getElementById("displayArea").innerHTML=cursor
    	}
    
    	function stopTracking(){
    		document.getElementById("displayArea").innerHTML="";
    	}
    
    	</script>
    </head>
    
    <body>
    	<div id="focusArea" onmousemove="getPos(event)" onmouseout="stopTracking()"><p>Mouse Over This Text And Get The Cursor Position!</p></div>
    	
    	<p id="displayArea"></p>
    </body>
    </html>



回答2:


The javascript method offset() used for tracking the position, and here I did the same as Mayur says, just little bit added. [jsfiddle.net/7P8Rr/][1]



来源:https://stackoverflow.com/questions/14545626/mouse-position-on-mouseover-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!