javascript - How to click and have x/y position written at that position? -
this question has answer here:
- how x&y corrdinates image input? 3 answers
i have image on page. want able click on image , @ point of click output x/y position (coordinates), each click. how can that?
note: know how x/y coordinate. question centers around how write x/y coordinates screen @ exact coordinates?
i suppose on click of image dynamically write inline div text coordinates in it. have no idea how implement that. thanks!
here working snippet. idea bind click event img
element , on click create dynamic span
tag , set top
, left
css property event.offsety
, event.offsetx
respectively. make sure span places click happened.
$('img').on('click',function(e){ var span = "<span style='position:absolute;top:"+e.pagey+"px;left:"+e.pagex+"px'>"+e.offsetx +","+ e.offsety+"</span>" $('body').append(span); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:and9gcrgxr0yfkbj07weot9eg1no2na2vsr8g5wxkrk4bkupudku-bwc8q" alt="my image" style="margin:50px;">
Comments
Post a Comment