I have an image on the screen and I need to click on the image and know the position
of where I clicked in relation to the X and Y of the image. For example, I clicked the center of the image, so X would be 50% and Y 50%.
I tried to do this:
let imgWidth = $("#fenimg").width();
let imgHeight = $("#fenimg").height();
$('#fenimg').click(function (e) {
var posX = $(this).offset().left
, posY = $(this).offset().top;
console.log(
((e.pageX - posX) * 100/imgWidth) + '% - ' +
((e.pageY - posY) * 100/imgHeight)
);
console.log(
((e.pageX) * 100/imgWidth) + '% - ' +
((e.pageY) * 100/imgHeight)
);
console.log(
((posX) * 100/imgWidth) + '% - ' +
((posY) * 100/imgHeight));
});
I also tried instead of offset()
using position()
but did not give the correct value.