Say I have a box on a web page, which is simply a div
styled using border: 1px solid black;
with a height and width of 200px.
I have 2 users, one is using google chrome on a laptop, the other is using an IPad, likely the devices have different screen resolutions, etc.
User 1 clicks in the center of the box on his google chrome. Then user 2 clicks the exact same spot on his IPad.
I want to determine in code if the users both clicked the same spot or not. I think if I tried to get the mouse coordinates, or even the top, left
etc, they will be different for User 1 and 2 even if they both clicked the exact same spot on the box, because their screen resolutions etc are different.
I used the following HTML:
Click anywhere in the box
<br />
<div style="border:1px solid black; min-height: 100px; min-width: 100px;
display: inline-block;" id="parent">
<span style="margin-top:40px; margin-left:40px; position: absolute;
cursor: pointer;" id="clickMe">X</span>
</div>
<br />
<div id="result"></div>
and the following Script:
$(document).ready(function () {
$("#parent").click(function (e) {
showEvent(e);
});
});
function showEvent(e) {
var props = ['offsetX', 'offsetY', 'pageX', 'pageY', 'screenX', 'screenY'];
var result = '';
for (var i = 0; i < props.length; i++) {
result += "<b>" + props[i] + "</b>:";
result += e[props[i]] + "<br />";
}
$("#result").html(result);
}
Here is the jsFiddle
I tried the above in google chrome & ff, and I get different values when i click the X in the center.
How can I resolve this? Is there a way to get the offset of the clicked part of the div relative to just the div and not the whole window? (E.g in this case the center of the div would be 100, so if the users both click 100px left and 100px top, they'd have clicked the same spot).
Any ideas?