Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the coordinates of a clicked mouse (I need math calculation only)

I have a box with a width of 800 and height of 600.

Then my screen size (container) is 1000 and its height is 700.

then, if we say:

x = container
y = rectangle
z = point in space

The engine only outputs the z based on its coordinate in x, therefore I need to calculate the coordinate of z in y.

I have:

z
size of x
size of y
coordinate of z in x

and what I want?

coordinate of z in y

like image 765
Mostafa Talebi Avatar asked Nov 22 '25 11:11

Mostafa Talebi


2 Answers

Click for Demo

Jquery

  $("#id").click(function(e){
       var parentOffset = $(this).parent().offset(); 
       //or $(this).offset(); if you really just want the current element's offset
       var relX = e.pageX - parentOffset.left;
       var relY = e.pageY - parentOffset.top;
    });

Html

<div id="id">
           //or $(this).offset(); if you really just want the current element's offset
</div>    
like image 113
codefreaK Avatar answered Nov 24 '25 02:11

codefreaK


Demo Link

It gives co-ordinates only for related container

Here is Script

$(document).ready(function(){
$("#container").click(function(e){
   alert(e.pageX - $("#container").parent().offset().left);
    alert( e.pageY - $("#container").parent().offset().top);
 });
});
like image 24
Harshal Patil Avatar answered Nov 24 '25 01:11

Harshal Patil