After clicking on an image on some point, I want to get the x and y coordinates of that point woth respect to the top left corner of the image.
点击某个点上的图像后,我想得到该点的x和y坐标相对于图像的左上角。
After googling and *ing a bit, I find this solution with jquery.
谷歌搜索和*ing后,我发现这个解决方案与jquery。
<html>
<script type="text/javascript" src="jquery.js"></script>
<body>
<!-- WEBSITE CONTENT -->
<img src="image.jpg" id="test">
<script>
$(document).ready(function() {
$('#test').click(function(e) {
var offset = $('#test').offset();
var x=e.pageX - offset.left;
var y=e.pageY - offset.top;
alert(x+' '+y);
});
});
</script>
</body>
</html>
Well, this code seems to work very well on non-mobile devices but it has problems on some mobile devices, in the sense that it does not return the right coordinates (at least pageY). On the other hand, even other solutions (based on the use of screenX/Y) seems to have problems on mobile devices.
好吧,这段代码似乎在非移动设备上运行良好,但它在某些移动设备上存在问题,因为它没有返回正确的坐标(至少是pageY)。另一方面,即使是其他解决方案(基于screenX / Y的使用)似乎在移动设备上也存在问题。
So, I'm wondering:
所以,我想知道:
1) somebody knows a robust solution working for mobile? Or,
1)有人知道一个适用于移动设备的强大解决方案吗?要么,
2) somebody knows a workaround to at least detect if the mobile device does not correctly interpret the code above?
2)有人知道一种解决方法,至少要检测移动设备是否没有正确解释上面的代码?
1 个解决方案
#1
0
Maybe page is scaled.When in windows, it return (2,2) maybe on mobile it return(1,1).
也许页面是缩放的。当在Windows中,它返回(2,2)可能在移动它返回(1,1)。
#1
0
Maybe page is scaled.When in windows, it return (2,2) maybe on mobile it return(1,1).
也许页面是缩放的。当在Windows中,它返回(2,2)可能在移动它返回(1,1)。