最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the exact mouse down coordinate? Why my code does not work? - Stack Overflow

programmeradmin1浏览0评论

I would like to show something at the mouse down place, I tried to use :

$('#my-graph').mousedown(function(evt){

      // show object at:
      var x= evt.screenX, y=evt.screenY;

      //or show object at:
     var x2=evt.pageX, y2=evt.pageY;

     //code to render object at (x,y) and (x2,y2)
     ......  
});

But neither of the above (x, y) and (x2,y2) place the rendered object at the mouse clicked place, and show the object some distance away from mouse down place, why?

I render the object with a position attributes, the position is relative to the #my-graph div, left up most corner of the div is supposed to be the origin point(0,0)

I would like to show something at the mouse down place, I tried to use :

$('#my-graph').mousedown(function(evt){

      // show object at:
      var x= evt.screenX, y=evt.screenY;

      //or show object at:
     var x2=evt.pageX, y2=evt.pageY;

     //code to render object at (x,y) and (x2,y2)
     ......  
});

But neither of the above (x, y) and (x2,y2) place the rendered object at the mouse clicked place, and show the object some distance away from mouse down place, why?

I render the object with a position attributes, the position is relative to the #my-graph div, left up most corner of the div is supposed to be the origin point(0,0)

Share Improve this question edited Sep 13, 2019 at 8:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 30, 2011 at 10:04 Leem.finLeem.fin 42.7k86 gold badges219 silver badges370 bronze badges 3
  • What do you expect the coordinates to mean? I mean, relative to what? The page, the screen or an element? – pimvdb Commented Sep 30, 2011 at 10:07
  • Do you render the object using proper position attributes? E.g. position: absolute in relevance to screen? – Przemek Commented Sep 30, 2011 at 10:07
  • Yes, I render the position with a position attributes, in relavant with the #my-graph div, left up most corner of the div is supposed to be the origin point – Leem.fin Commented Sep 30, 2011 at 10:09
Add a ment  | 

1 Answer 1

Reset to default 5

You seem to want offsetX and offsetY: http://jsfiddle/f52Gg/.

$("div").mousedown(function(e){
    alert(e.offsetX + " " + e.offsetY);
});
发布评论

评论列表(0)

  1. 暂无评论