I am excited to experiment with toast notifications by creating them dynamically. Each toast has a unique id number and I increment a counter every time a new one is created.
The current functionality includes the toast sliding down, staying visible for 3 seconds, and then automatically getting removed from the screen.
var thisToast = this.toastCounter - 1;
$(document).find("#toast_" + thisToast).slideDown(500);
setTimeout(function() {
$(document).find("#toast_" + thisToast).remove();
}, 3000);
My goal now is to enhance the toast behavior by making it fade in, stay on display for a specified duration (X seconds), fade out gracefully, and then self-destruct.
An important requirement is that when multiple toast messages are generated, each new one should be positioned below its predecessor for clear visibility.
If you require a comprehensive view of my code implementation, here it is:
CreateToast(isValid) {
var toastMessage;
var foreColor;
var backgroundColorIconDiv
var backgroundColorContentDiv;
var borderColor;
if (!isValid) {
toastMessage = "Failure";
foreColor = "#ff0000";
backgroundColorIconDiv = "#ff8080";
backgroundColorContentDiv = "#ff471a";
borderColor = "#800000";
} else {
toastMessage = "Success";
foreColor = "#2fb62f";
backgroundColorIconDiv = "#71da71";
backgroundColorContentDiv = "#00e673";
borderColor = "#00802b";
}
this.CreateWrapper(toastMessage, foreColor, borderColor, backgroundColorIconDiv, backgroundColorContentDiv);
this.toastCounter++;
var thisToast = this.toastCounter - 1;
$(document).find("#toast_" + thisToast).slideDown(500);
setTimeout(function() {
$(document).find("#toast_" + thisToast).remove();
}, 3000);
}
CreateWrapper(toastMessage, foreColor, borderColor, backgroundColorIconDiv, backgroundColorContentDiv) {
var wrapperDiv = document.createElement("div");
wrapperDiv.id = "toast_" + this.toastCounter;
wrapperDiv.style.display = "none";
wrapperDiv.style.margin = "0 auto";
wrapperDiv.style.width = "200px";
wrapperDiv.style.height = "50px";
wrapperDiv.style.border = "2px solid " + borderColor;
wrapperDiv.style.borderRadius = "10px";
document.body.appendChild(wrapperDiv);
this.CreateIconDiv(wrapperDiv, backgroundColorIconDiv);
this.CreateContentDiv(wrapperDiv, toastMessage, foreColor, backgroundColorContentDiv);
}
CreateIconDiv(parentDiv, backgroundColor) {
var iconDiv = document.createElement("div");
iconDiv.style.textAlign = "center";
iconDiv.style.width = "20%";
iconDiv.style.cssFloat = "left";
iconDiv.style.backgroundColor = backgroundColor;
parentDiv.appendChild(iconDiv);
}
CreateContentDiv(parentDiv, toastMessage, foreColor, backgroundColor) {
var contentDiv = document.createElement("div");
contentDiv.style.textAlign = "center";
contentDiv.style.width = "80%";
contentDiv.style.cssFloat = "right";
contentDiv.style.color = foreColor;
contentDiv.style.backgroundColor = backgroundColor;
contentDiv.style.fontWeight = "bold";
contentDiv.style.fontSize = "20px";
contentDiv.innerHTML = toastMessage;
parentDiv.appendChild(contentDiv);
}
Your assistance with this would be greatly appreciated :)