kamran Ladhani kamran Ladhani - 5 months ago 11
HTML Question

Mouse position not working for FireFox

I am stuck in an issue. I have to fetch mouse positions on firefox browser. However it is not working may be I am doing any mistake in code. So far I have done is given below.

Javascript Code :

function MousePos(event){
if ($.browser.mozilla == true){
if(typeof event.offsetX === "undefined" || typeof event.offsetY === "undefined"{
var targetOffset = $(event.target).offset();
event.offsetX = event.pageX - targetOffset.left;
event.offsetY = event.pageY - targetOffset.top;
alert(event.offsetX + " " + event.offsetY);
}
}
}


HTML Code :

<div class="paymentTracker" onmouseover="MousePos();">

</div>


The function is working if I show an alert box only but this code having issue. I want mouse positions only on Firefox browser.
Thanks in advance.

Answer

try this : I think its not working because its not taking onmouseover function you have defined in html.

$( ".paymentTracker" ).mouseover(function(event) {

             var x = event.clientX;
var y = event.clientY;
var coords = "X coords: " + x + ", Y coords: " + y;
             alert(coords);

});


<div class="paymentTracker"></div>

 <style>
.paymentTracker {width:300px; height:300px;border:1px solid;}
 </style>

heres the fiddle : https://jsfiddle.net/0yptrjdw/

with your code

$( ".paymentTracker" ).mouseover(function(event) {
var targetOffset = $(event.target).offset();
             event.offsetX = event.pageX - targetOffset.left;
             event.offsetY = event.pageY - targetOffset.top;
             alert(event.offsetX + "   " + event.offsetY);


});
Comments