LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Simple Customizable Toast Notification In Vanilla JavaScript

Simple Customizable Toast Notification In Vanilla JavaScript

Simple-Customizable-Toast-Notification-In-Vanilla-JavaScript
This JavaScript code snippet helps you to create a simple toast notification. It allows you to display various types of alerts including success, warning, and danger with predefined themes. The styles and notifications can be customized according to your needs. The toast can be triggered with any event and closed with the close button or after a specific time.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Simple Customizable Toast Notification In Vanilla JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<link href="https://cdn.lineicons.com/3.0/lineicons.css" rel="stylesheet">
<style type="text/css">
*{
margin:0;
padding:0;
}
.toast-notification{
min-width: 200px;
height:50px;
background-color:white;
border-radius: 10px;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
bottom:0;
margin-bottom:20px;
margin-left:0;
position:fixed;
z-index:1;
display:flex;
flex-direction:row;
}
.message-container{
width:80%;
padding-top:13px;
padding-left: 20px;
font-family:'Roboto';
color:white;
}
.close-notification{
width:20%;
}
.close-notification > i {
padding-top:15px;
padding-left:5px;
font-weight:900;
color:white;
cursor:pointer;
}
</style>

HTML:

<button id="toast-button-succes" style="background-color:#6c5ce7;width:150px;height:50px;border:none;border-radius:5px;margin-top:100px;margin-left:100px;color:white;font-size:20px;cursor:pointer;box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;">Toast Succes</button>
<button id="toast-button-warning" style="background-color:#6c5ce7;width:150px;height:50px;border:none;border-radius:5px;margin-top:100px;margin-left:100px;color:white;font-size:20px;cursor:pointer;box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;">Toast Warning</button>
<button id="toast-button-danger" style="background-color:#6c5ce7;width:150px;height:50px;border:none;border-radius:5px;margin-top:100px;margin-left:100px;color:white;font-size:20px;cursor:pointer;box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;">Toast Danger</button>

JavaScript:

<script type="text/javascript">
class Toast {
constructor(message,color,time){
this.message = message;
this.color = color;
this.time = time;
this.element = null;
var element = document.createElement('div');
element.className = "toast-notification";
this.element = element;
var countElements = document.getElementsByClassName("toast-notification");
element.style.opacity=0.8;
element.style.marginBottom = (countElements.length * 55) + "px";
element.style.backgroundColor = this.color;
var message = document.createElement("div");
message.className = "message-container";
message.textContent = this.message;
element.appendChild(message);
var close = document.createElement("div");
close.className = "close-notification";
var icon = document.createElement("i");
icon.className = "lni lni-close";
close.appendChild(icon);
element.append(close);
document.body.appendChild(element);
setTimeout(function() {
element.remove();
}, this.time);
close.addEventListener("click",()=>{
element.remove();
})
}
}
const ToastType = {
Danger : "#eb3b5a",
Warning: "#fdcb6e",
Succes : "#00b894",
}
var count = 0;
document.querySelector("#toast-button-succes").addEventListener("click",()=>{
new Toast("Toast Message " + count,ToastType.Succes,5000);
count++;
})
document.querySelector("#toast-button-warning").addEventListener("click",()=>{
new Toast("Toast Message " + count,ToastType.Warning,5000);
count++;
})
document.querySelector("#toast-button-danger").addEventListener("click",()=>{
new Toast("Toast Message " + count,ToastType.Danger,5000);
count++;
})
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
Multiple Pages As Show/Hide DIV On A Single Web Page Using Pure JavaScript

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *