You're probably looking for clientX
and clientY
, and not pageX
and pageY
. Also, notice that you must put position: absolute
on the modal you're trying to place so it'll work as you're expecting it to (and if you're not too familiar with positioning you should definitely read about it - it's not as simple as it sounds).
pageX
and pageY
:
Relative to the top left of the fully rendered content area in the browser [...] This point could be anywhere in the browser window and can actually change location if there are embedded scrollable pages embedded within pages and the user moves a scrollbar.
screenX
and screenY
:
Relative to the top left of the physical screen/monitor.
clientX
and clientY
:
Relative to the upper left edge of the content area (the viewport) of the browser window. This point does not move even if the user moves a scrollbar from within the browser.
Read more:
Here's a working example:
document.addEventListener("click", e => {
console.log(e.clientX, e.clientY);
let modal = document.getElementById("modal");
modal.style.top = e.clientY+"px";
modal.style.left = e.clientX+"px";
});
#modal {
position: absolute;
width: 100px;
height: 200px;
background: blue;
}
<div id="modal"></div>