website/js/dialog.js

37 lines
1,021 B
JavaScript
Raw Normal View History

2021-03-24 22:18:55 +01:00
function createDialogImage(url) {
createDialogHTML(`<img src="${url}">`, "imageDialog");
}
2021-03-24 22:18:55 +01:00
function createDialogHTML(html, customClasses = "") {
2021-03-09 22:35:57 +01:00
let dialog = document.createElement("div");
dialog.classList.add("dialog");
dialog.innerHTML = `
2021-03-22 21:03:09 +01:00
<span class="close">&times;</span>
2021-03-09 22:35:57 +01:00
<div class="dialogContent ${customClasses}">
${html}
</div>`;
document.body.appendChild(dialog);
fade(dialog, 0.04);
2021-03-22 21:03:09 +01:00
2021-03-24 22:18:55 +01:00
dialog.querySelector(".close").onclick = function (){
2021-03-22 21:03:09 +01:00
fade(dialog, -0.04, true);
}
}
2021-03-24 22:18:55 +01:00
function fade(element, value = 0.1, deleteAfterwards = false) {
let opacity = +(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 () {
element.parentNode.removeChild(element);
}, 10);
}
}