I'm a student and it's my first time here !
So I'm trying to code a toast notification for the first time, here's mine : toast notification
The problem is, when I add several of them, after 5s, they go below everything. I wanted it to be like this but after they all disappeared. And I don't know how to add like a personal disappearance time to each of the toast notification. So when one disappears, the others are like that : problematic toast notifications
The container has to disappear because either way I can't click on the button add to cart that are below it.
Do you have an idea for how to fix this ? If you have any code advice too, don't hesitate to tell me !
Here's the code (a little chaotic because I tried to follow youtube videos and then add my own thing and then I tried to find a solution with Gemini (Google AI) so yeah... ) :
function toastNotification(msg) {
const alertContainer = document.querySelector(".container-alert"); // Select the container
const newAlert = document.createElement("div"); // Create a new alert element
newAlert.classList.add("alert", "showAlert"); // Add necessary classes
// ...
const closeBtn = document.createElement("div");
closeBtn.classList.add("close-btn");
const closeIcon = document.createElement("i");
closeIcon.classList.add("fas", "fa-times");
closeBtn.appendChild(closeIcon);
newAlert.appendChild(closeBtn);
// Append the new alert to the container
alertContainer.appendChild(newAlert);
// Add animation for showing and hiding the alert
newAlert.classList.add("show");
alertContainer.style.zIndex = "9999";
setTimeout(() => {
newAlert.classList.remove("show");
newAlert.classList.add("hide");
// Remove the alert after hiding animation
setTimeout(() => {
alertContainer.removeChild(newAlert);
alertContainer.style.zIndex = "-1";
}, 5000);
}, 5000);
closeBtn.addEventListener("click", () => {
newAlert.classList.remove("show");
newAlert.classList.add("hide");
// Immediately remove the alert if closed manually
setTimeout(() => {
alertContainer.removeChild(newAlert);
alertContainer.style.zIndex = "-1";
}, 5000);
});
}
toastNotification("hello world")
setTimeout(() => toastNotification("hello world2"), 1000)
.container-alert {
width: 30rem;
position: fixed;
right: 0;
bottom: 0px;
display: flex;
align-items: center;
justify-content: end;
flex-direction: column;
overflow: hidden;
padding: 20px;
z-index: -1;
}
.alert {
overflow: hidden;
opacity: 0;
pointer-events: none;
width: 27rem;
height: 5rem;
font-weight: 500;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
display: flex;
align-items: center;
}
.alert.showAlert {
opacity: 1;
pointer-events: auto;
background-color: #fff;
}
<div class="container-alert">
<div class="alert hide">
<svg xmlns="http://www.w3.org/2000/svg" width="1.5rem" height="1.5rem" viewBox="0 0 48 48">
<defs>
<mask id="ipSCheckOne0">
<g fill="none" stroke-linejoin="round" stroke-width="4">
<path fill="#fff" stroke="#fff" d="M24 44a19.937 19.937 0 0 0 14.142-5.858A19.937 19.937 0 0 0 44 24a19.938 19.938 0 0 0-5.858-14.142A19.937 19.937 0 0 0 24 4A19.938 19.938 0 0 0 9.858 9.858A19.938 19.938 0 0 0 4 24a19.937 19.937 0 0 0 5.858 14.142A19.938 19.938 0 0 0 24 44Z"/>
<path stroke="#000" stroke-linecap="round" d="m16 24l6 6l12-12"/>
</g>
</mask>
</defs>
<path fill="#30d300" d="M0 0h48v48H0z" mask="url(#ipSCheckOne0)"/>
</svg>
<span class="msg"></span>
<div class="close-btn">
<span class="fas fa-times"></span>
</div>
</div>
</div>
I found the solution !
So when you call the toastNotification function, you make the z-index -1 a line after. Not in the function like I did. So I deleted the z-index line that was on the timeout and moved it where it should've been !