我在浏览器中创建了一个div,在div中添加了一张图片(图片是从后台传过来的),我是监听鼠标点击图片,获取鼠标点击图片时的屏幕坐标,但是在图片上点击同一个点返回的屏幕坐标确不相同,希望大家给看看:
window.onload=function()
{ obj = document.getElementById("imgmap");//找到对象
obj.onmousedown = down;
}
//设置鼠标单击监听函数
function down(event)
{
oEvent=event||window.event;
if(oEvent.button==1) {
firx=oEvent.clientX-10; //这个函数的作用要图片的起始点为(0,0)
firy=oEvent.clientY-10;
document.getElementById("ttc").value=firx;
document.getElementById("hubj").value=firy;
}
}
<div id="imp" style="width:800px; height:500px; border:1px solid #000; overflow:hidden;">
<img id="imgmap" src="maptest" >
</div>
<input type="text" name ="ttc" id="ttc">
<input type="text" name ="hubj" id="hqbj">
每次点击图片上同一个点返回的屏幕坐标不同,这是为什么啊??
window.onload=function()
{ obj = document.getElementById("imgmap");//找到对象
obj.onmousedown = down;
}
//设置鼠标单击监听函数
function down(event)
{
oEvent=event||window.event;
if(oEvent.button==1) {
firx=oEvent.clientX-10; //这个函数的作用要图片的起始点为(0,0)
firy=oEvent.clientY-10;
document.getElementById("ttc").value=firx;
document.getElementById("hubj").value=firy;
}
}
<div id="imp" style="width:800px; height:500px; border:1px solid #000; overflow:hidden;">
<img id="imgmap" src="maptest" >
</div>
<input type="text" name ="ttc" id="ttc">
<input type="text" name ="hubj" id="hqbj">
每次点击图片上同一个点返回的屏幕坐标不同,这是为什么啊??
另外代码兼容性好象有点问题,firefox11就无法正常工作
$('#imgmap').click(function(e) {
var offset = $(this).offset();
alert(e.clientX - offset.left);//相对于图的坐标,如果相对于屏幕的坐标,直接e.clientX
alert(e.clientY - offset.top);
});
});