6
I have an image on the screen and I need to click on the image and know the position
where I clicked on the X and Y of the image. For example, I clicked on the center of the image, then the X would be 50% and the Y 50%.
I tried to do so:
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 tried too instead of offset()
use the position()
but did not give the correct value.
Thank you @Aline
– Alisson Acioli