VanillaToasts is a lightweight and easy-to-use vanilla JavaScript library for displaying toast-style notifications on your website / web application.
Create toasts & notifications on your website with ease. This library is extremely lightweight and depends on no other library. Simply load the script and css to your page, and use the simple API to start launching toasts on your page.
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.
Table of Contents
Features:
- Light Weight.
- Pure HTML-CSS-JSCode.
- Cross Browser.
- No JQuery Files.
- Fully Customizable.
- Responsive.
How To Create Simple And Rich Toast-Style Web Notifications 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:
<style type="text/css"> #vanillatoasts-container{position:fixed;top:0;right:0;width:320px;font-family:helvetica}.vanillatoasts-toast{position:relative;padding:20px 17px;margin:20px;border-radius:10px;background:#f5f5f5;cursor:pointer;box-shadow:1px 1px 3px rgba(0,0,0,.1);animation-duration:.3s;animation-name:VanillaToasts;animation-timing-function:cubic-bezier(.215,.61,.355,1)}.vanillatoasts-fadeOut{animation-name:VanillaToastsFadeOut;animation-duration:.3s;animation-timing-function:cubic-bezier(.215,.61,.355,1);animation-fill-mode:forwards}#vanillatoasts-container p,h4{margin:3px 0!important}.vanillatoasts-title{font-weight:700;font-size:15px;margin-bottom:10px}.vanillatoasts-text{font-size:14px;color:#777}.vanillatoasts-icon{position:absolute;top:5px;left:-40px;width:50px;height:50px;border-radius:100%;box-shadow:1px 1px 3px rgba(0,0,0,.1);background:#fff}.vanillatoasts-toast a,.vanillatoasts-toast a:hover{color:#549edb!important;text-decoration:none!important}.vanillatoasts-success{border-bottom:2px solid #51c625}.vanillatoasts-warning{border-bottom:2px solid #db9215}.vanillatoasts-error{border-bottom:2px solid #db2b1d}.vanillatoasts-info{border-bottom:2px solid #27abdb}@keyframes VanillaToasts{from{transform:translate3d(400px,0,0);opacity:0}to{transform:translate3d(0,0,0);opacity:1}}@keyframes VanillaToastsFadeOut{from{transform:translate3d(0,0,0);opacity:1}to{transform:translate3d(400px,0,0);opacity:0}} </style>
JavaScript:
<script type="text/javascript"> (function(root,factory){if(typeof exports==='object'){module.exports=factory();}else{root.VanillaToasts=factory();}})(this,function(){if(document.readyState==='complete'){init();}else{window.addEventListener('DOMContentLoaded',init);} function init(){var container=document.createElement('div');container.id='vanillatoasts-container';document.body.appendChild(container);} var VanillaToasts={};var autoincrement=0;VanillaToasts.create=function(options){var toast=document.createElement('div');toast.id=++autoincrement;toast.id='toast-'+toast.id;toast.className='vanillatoasts-toast';if(options.title){var h4=document.createElement('h4');h4.className='vanillatoasts-title';h4.innerHTML=options.title;toast.appendChild(h4);} if(options.text){var p=document.createElement('p');p.className='vanillatoasts-text';p.innerHTML=options.text;toast.appendChild(p);} if(options.icon){var img=document.createElement('img');img.src=options.icon;img.className='vanillatoasts-icon';toast.appendChild(img);} if(typeof options.callback==='function'){toast.addEventListener('click',options.callback);} toast.hide=function(){toast.className+=' vanillatoasts-fadeOut';toast.addEventListener('animationend',removeToast,false);};if(options.timeout){setTimeout(toast.hide,options.timeout);} if(options.type){toast.className+=' vanillatoasts-'+options.type;} toast.addEventListener('click',toast.hide);function removeToast(){document.getElementById('vanillatoasts-container').removeChild(toast);} document.getElementById('vanillatoasts-container').appendChild(toast);return toast;};return VanillaToasts;}); </script>
Usage JavaScript:
<script type="text/javascript> VanillaToasts.create({ // notification title title: 'Welcome to my site', // notification message text: 'This toast will hide after 5000ms or when you click it', // success, info, warning, error / optional parameter type: 'warning', // path to notification icon icon: '/img/alert-icon.jpg', // topRight, topLeft, topCenter, bottomRight, bottomLeft, bottomCenter positionClass: 'bottomLeft', // auto dismiss after 5000ms timeout: 5000, // hide any other toasts, keeping only the latest visible single: true, // executed when toast is clicked callback: function() { ... } }); toast.hide(); // Hide Manually On Click Whereever You Want VanillaToasts.setTimeout(toast.id, 2000); // Hide Automatically On Click Whereever You Want </script>
Customization:
No need to customize it. Just copy-paste. Rest edit the code as per comments and need.
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.
Be the first to write a comment.