Javascript 使用Jquery获取单击图像的坐标
我有一个调整大小的图像(width=“100”)和一个jquery脚本,在单击该图像时输出当前坐标Javascript 使用Jquery获取单击图像的坐标,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个调整大小的图像(width=“100”)和一个jquery脚本,在单击该图像时输出当前坐标 <img id="image" src="http://localhost/image.jpg" width="100" > <script> $('#image').mousemove( function(event) { window.current_x = Math.round(event.pageX - $('#image').offset().
<img id="image" src="http://localhost/image.jpg" width="100" >
<script>
$('#image').mousemove( function(event) {
window.current_x = Math.round(event.pageX - $('#image').offset().left) ;
window.current_y = Math.round(event.pageY - $('#image').offset().top);
window.current_coords = window.current_x + ', ' + window.current_y;
$('#edit_instants_now').html('Current position: ' + window.current_coords + '.');
}).mouseleave( function() {
$('#edit_instants_now').html(' ');
}).click( function() {
$('#edit_instants_click').html('Last click: ' + window.current_coords + '. ');
document.edit_instant.edit_instant_x.value = window.current_x;
document.edit_instant.edit_instant_y.value = window.current_y;
});
</script>
$('#image').mousemove(函数(事件){
window.current_x=Math.round(event.pageX-$('#image').offset().left);
window.current_y=Math.round(event.pageY-$('#image').offset().top);
window.current\u coords=window.current\u x+,'+window.current\u y;
$('edit#instants_now').html('Current position:'+window.Current_coords+');
}).mouseleave(函数(){
$('#编辑即时消息''.html('');
})。单击(函数(){
$('edit#instants_click').html('Last click:'+window.current_coords+');
document.edit\u instant.edit\u instant\u x.value=window.current\u x;
document.edit\u instant.edit\u instant\u y.value=window.current\u y;
});
问题是我想得到原始图像的实际坐标,而不是调整大小的图像
你有什么建议吗?谢谢。获取原始图像的大小,将其除以大小调整后的图像的大小-这将为您提供一个
比例因子
,然后将单击大小调整后的图像的x、y坐标乘以比例因子
希望这有帮助。
var naturalWidth=100;
$('#image').mousemove(函数(事件){
var img=$(“#图像”);
console.log(自然宽度);
比率=自然宽度/img.宽度();
window.current_x=(event.pageX-img.offset().left)*比率;
window.current_y=(event.pageY-img.offset().top)*比率;
window.current\u coords=window.current\u x+,'+window.current\u y;
$('edit#instants_now').html('Current position:'+window.Current_coords+');
}).mouseleave(函数(){
$('#编辑即时消息''.html('');
})。单击(函数(){
$('edit#instants_click').html('Last click:'+window.current_coords+');
document.edit\u instant.edit\u instant\u x.value=window.current\u x;
document.edit\u instant.edit\u instant\u y.value=window.current\u y;
});
$('img')。on('load',函数(e){
自然宽度=e.target.naturalWidth;
})
您知道原始尺寸吗?如果是这样的话,你可以直接插值。如果不是这样的话,有几种方法可以得到:大小不总是一样的。它有各种尺寸。即使我知道尺寸,我怎么能得到坐标?我是jquery新手。这是基本代数:如果单击的坐标是X,调整的宽度是W;原始宽度是A,原始坐标是X/W*AThanks!!成功了!