Make clicking also show zip code
This commit is contained in:
parent
ffc497cdbd
commit
0bed907c52
36
script.js
36
script.js
@ -6,19 +6,36 @@ 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}<br>${coordY}`;
|
||||
|
||||
var oldMouseX = mouseX, oldMouseY = mouseY;
|
||||
setTimeout(() => {
|
||||
if (!mouseX || !mouseY || oldMouseX != mouseX || oldMouseY != mouseY) return;
|
||||
|
||||
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}<br>${coordY}`;
|
||||
|
||||
var xhr = new XMLHttpRequest;
|
||||
xhr.onload = () => {
|
||||
if (xhr.status === 200)
|
||||
@ -28,12 +45,7 @@ denmark.onmousemove = event => {
|
||||
}
|
||||
xhr.open("GET", `https://api.dataforsyningen.dk/postnumre/reverse?x=${coordX}&y=${coordY}&callback=onZipFound`);
|
||||
xhr.send();
|
||||
}, 200);
|
||||
};
|
||||
|
||||
denmark.onmouseleave = () => {
|
||||
mouseX = mouseY = null;
|
||||
};
|
||||
}
|
||||
|
||||
function onZipFound(data) {
|
||||
zip.innerHTML = `${data.nr} <b>${data.navn}</b>`;
|
||||
|
Loading…
Reference in New Issue
Block a user