Given absolute or relative position (top & left) is there any way to get the nearest html element to these co-ordinates?
Or alternately, is there any way to craf
I used @Felix answer and applied it to vanilla Js for my requirements, I know it's an old question but if anyone needs it
function getClosestChoice(x, y, elements) {
let closestEl,
minDist,
offset;
elements.forEach((el) => {
offset = { left: el.offsetLeft, top: el.offsetTop };
offset.left += el.offsetWidth / 2;
offset.top += el.offsetHeight / 2;
const dist = Math.sqrt(
(offset.left - x) * (offset.left - x) + (offset.top - y) * (offset.top - y)
);
if (!minDist || dist < minDist) {
minDist = dist;
closestEl = el;
}
});
return closestEl;
}