如何获得点击鼠标的坐标(我只需要数学计算)

时间:2021-12-09 20:32:14

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

我有一个宽度为800,高度为600的盒子。

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

然后我的屏幕尺寸(容器)是1000,其高度是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.

引擎只根据x中的坐标输出z,因此我需要计算z中y的坐标。

I have:

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

and what I want?

我想要什么?

coordinate of z in y

y的坐标

2 个解决方案

#1


4  

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>    

#2


1  

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);
 });
});

#1


4  

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>    

#2


1  

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);
 });
});