Just get click position relative to parent tag, and then move the child tag using style left/top.
// get reference let outer = document.getElementById("container"); let inner = document.getElementById("circle"); // add click listener to parent tag outer.addEventListener("click", function(event) { let result = { Azimut: { x: event.offsetX, y: event.offsetY, }, Size: 10, } // inner.style.top = (result.Azimut.y - result.Size / 2) + "px"; // inner.style.left = (result.Azimut.x - result.Size / 2) + "px"; inner.style.width = result.Size + "px"; inner.style.height = result.Size + "px"; })
<div id="container" style=" width: 100px; height: 100px; border: 1px solid black; position: relative;"> <div id="circle" style=" border: 1px solid black; position: absolute; "></div> <div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.