From 0bed907c523cc77ef005ed3a7e7d30bc581f7405 Mon Sep 17 00:00:00 2001 From: ReiMerc Date: Mon, 6 Feb 2023 13:29:29 +0100 Subject: [PATCH] Make clicking also show zip code --- script.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index 606aea0..4f019a3 100644 --- a/script.js +++ b/script.js @@ -6,35 +6,47 @@ denmark.onmousemove = event => { zip.style.display = "none"; var rect = denmark.getBoundingClientRect(); - mouseX = event.x - rect.left; mouseY = event.y - rect.top; - var coordX = mouseX / event.target.clientWidth * 8 + 6.2; - var coordY = Math.abs(mouseY / event.target.clientHeight * 3.6 - 57.93); - - coords.innerHTML = `${coordX}
${coordY}`; - var oldMouseX = mouseX, oldMouseY = mouseY; setTimeout(() => { if (!mouseX || !mouseY || oldMouseX != mouseX || oldMouseY != mouseY) return; - var xhr = new XMLHttpRequest; - xhr.onload = () => { - if (xhr.status === 200) - eval(xhr.responseText); - else - onZipError(); - } - xhr.open("GET", `https://api.dataforsyningen.dk/postnumre/reverse?x=${coordX}&y=${coordY}&callback=onZipFound`); - xhr.send(); + showZipCode(); }, 200); }; +denmark.onclick = event => { + var rect = denmark.getBoundingClientRect(); + mouseX = event.x - rect.left; + mouseY = event.y - rect.top; + + ShowZipCode(); +}; + + denmark.onmouseleave = () => { mouseX = mouseY = null; }; +function showZipCode() { + var coordX = mouseX / denmark.clientWidth * 8 + 6.2; + var coordY = Math.abs(mouseY / denmark.clientHeight * 3.6 - 57.93); + + coords.innerHTML = `${coordX}
${coordY}`; + + var xhr = new XMLHttpRequest; + xhr.onload = () => { + if (xhr.status === 200) + eval(xhr.responseText); + else + onZipError(); + } + xhr.open("GET", `https://api.dataforsyningen.dk/postnumre/reverse?x=${coordX}&y=${coordY}&callback=onZipFound`); + xhr.send(); +} + function onZipFound(data) { zip.innerHTML = `${data.nr} ${data.navn}`; zip.style.left = (mouseX+3) + "px";