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

时间:2014-04-12 12:04:57

标签: javascript jquery math

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

然后我的屏幕尺寸(容器)为1000,高度为700。

然后,如果我们说:

x = container
y = rectangle
z = point in space

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

我有:

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

我想要什么?

z

y的坐标

2 个答案:

答案 0 :(得分: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>    

答案 1 :(得分:1)

Demo Link

它仅为相关容器提供坐标

这是脚本

$(document).ready(function(){
$("#container").click(function(e){
   alert(e.pageX - $("#container").parent().offset().left);
    alert( e.pageY - $("#container").parent().offset().top);
 });
});
相关问题