website/ts/dialog.ts
2021-03-22 21:03:09 +01:00

38 lines
No EOL
1.1 KiB
TypeScript

function createDialogImage(url:string) {
createDialogHTML(`<img src="${url}">`, "imageDialog");
}
function createDialogHTML(html:string, customClasses = "") {
let dialog = document.createElement("div");
dialog.classList.add("dialog");
dialog.innerHTML = `
<span class="close">&times;</span>
<div class="dialogContent ${customClasses}">
${html}
</div>`;
document.body.appendChild(dialog);
fade(dialog, 0.04);
(<HTMLElement> dialog.querySelector(".close")).onclick = function (){
fade(dialog, -0.04, true);
}
}
function fade(element:HTMLElement, value = 0.1, deleteAfterwards = false) {
let opacity:number = +(element.style.opacity) + value;
element.style.opacity = String(opacity);
if ((opacity < 1 && value > 0) || (opacity >= 0 && value < 0)) {
setTimeout(function () {
fade(element, value, deleteAfterwards);
}, 10);
} else if (deleteAfterwards) {
setTimeout(function () {
// @ts-ignore
element.parentNode.removeChild(element);
}, 10);
}
}