Toast Messages and Notifications Standalone Library no JQuery - notify.js
What is notify.js?
This is a small standalone javascript library that allows you to attach toast type messages that appear in the top right corner of your display with selected messages.
Preview
a preview of notify.js
What can i use this for?
If you have some messages that you want the user to see, that do not get in the way too much this could be a nice option. The only Issue i found with this is that maybe the messages do not appear long enough and if you miss a message there is no way to get it back.
Conclusion
A great small message library, with 5kb of CSS and 2kb of javascript, it is very lightweight and should not add much loading time to your project.
Original Source
Toast Messages and Notifications Standalone Library no JQuery - notify.js Demo
View Demo Full Screen View Demo New Tab
Toast Messages and Notifications Standalone Library no JQuery - notify.js Code
HTML
<!-- The Buttons to Launch the Toast Messages -->
<button class="btnLaunch1">Info with content</button>
<button class="btnLaunch2">Success with content</button>
<button class="btnLaunch3">Danger with sound</button>
<button class="btnLaunch4">Warning</button>
<!-- The Buttons to Launch the Toast Messages -->
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
<p>Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.</p>
CSS
.btnLaunch1, .btnLaunch2, .btnLaunch3, .btnLaunch4 {
padding: 10px;
margin: 10px;
background-color: #fff;
border: 2px solid #0057fa;
border-radius: 100px;
}
.btnLaunch1:hover, .btnLaunch2:hover, .btnLaunch3:hover, .btnLaunch4:hover {
border: 2px solid #002261;
}
/* Notify container */
#notifyContainer {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: end;
-ms-flex-pack: end;
justify-content: flex-end;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
position: fixed;
margin: 0 auto;
padding: 0;
z-index: 999;
max-width: 320px;
}
.top_right {
top: 0;
left: 50%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
-webkit-box-orient: vertical;
-webkit-box-direction: reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
}
.top_left {
top: 0;
left: 50%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
-webkit-box-orient: vertical;
-webkit-box-direction: reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
}
.bottom_right {
top: 0;
left: 50%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
-webkit-box-orient: vertical;
-webkit-box-direction: reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
}
.bottom_left {
top: 0;
left: 50%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
-webkit-box-orient: vertical;
-webkit-box-direction: reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
}
/* Notify */
.alerts {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: start;
-ms-flex-align: start;
align-items: flex-start;
margin: 10px 0 0 0;
padding: 0 10px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
font-family: sans-serif;
width: 100%;
border-radius: 4px;
-webkit-box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
font-size: 12.5px;
-webkit-transition: all 0.3 ease-out;
transition: all 0.3 ease-out;
}
.alerts {
-webkit-animation: fadeIn .3s ease-in 0s 1 forwards;
animation: fadeIn .3s ease-in 0s 1 forwards;
}
.alerts-success {
background-color: rgba(209, 231, 221, 0.96);
color: #0f5132;
}
.alerts-danger {
background-color: rgba(248, 215, 218, 0.96);
color: #842029;
}
.alerts-warning {
background-color: rgba(255, 243, 205, 0.96);
color: #664d03;
}
.alerts-info {
background-color: rgba(207, 244, 252, 0.96);
color: #055160;
}
.alerts > h3 {
width: 100%;
display: inline-block;
font-size: 12.5px;
margin: 0;
padding: 10px 0;
line-height: 1.5;
}
.alerts > p {
width: 100%;
min-height: 0px;
display: inline-block;
font-size: 12.5px;
padding: 0 0 10px;
margin: 0;
}
.fadeOut {
-webkit-animation: fadeOut .3s ease-out 0s 1 forwards;
animation: fadeOut .3s ease-out 0s 1 forwards;
}
@-webkit-keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-webkit-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
button:active, button:focus {
outline: none;
}
button::-moz-focus-inner {
border: 0;
}
@media (min-width: 769px) {
.top_right {
top: 0;
bottom: auto;
right: 10px;
left: auto;
-webkit-transform: none;
transform: none;
-webkit-box-orient: vertical;
-webkit-box-direction: reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
}
.top_left {
top: 0;
bottom: auto;
right: auto;
left: 10px;
-webkit-transform: none;
transform: none;
-webkit-box-orient: vertical;
-webkit-box-direction: reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
}
.bottom_right {
top: auto;
bottom: 0;
right: 10px;
left: auto;
-webkit-transform: none;
transform: none;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
.bottom_left {
top: auto;
bottom: 0;
right: auto;
left: 10px;
-webkit-transform: none;
transform: none;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
}
Javascript
/* notify.js */
document.body.click()
// const Notify = (o)=> {
function Notify(o) {
// default params
if (!o) o = {}
if (!o.corner) o.corner = "top_right";
if (!o.delay) o.delay = 5000;
if (!o.maxElem) o.maxElem = 10;
const notifyContainer = document.createElement("div");
notifyContainer.id = "notifyContainer";
notifyContainer.className = o.corner;
document.body.appendChild(notifyContainer);
const countAlerts = document.getElementsByClassName("alerts");
this.notifyCreate = (style, head, content, sound) => {
let alerts = document.createElement("div");
alerts.className = "alerts";
alerts.className += " alerts-" + style;
if (!content) {
alerts.innerHTML = `<h3>${head}</h3>`;
} else {
alerts.innerHTML = `<h3>${head}</h3><p>${content}</p>`;
};
notifyContainer.appendChild(alerts);
if (sound) {
const audio = new Audio('../assets/sound/notification.mp3');
audio.play();
}
setTimeout(function () {
alerts.classList.toggle("fadeOut");
}, o.delay - 300);
setTimeout(function () {
notifyContainer.removeChild(alerts);
}, o.delay);
};
this.render = (r) => {
// default params
if (!r) r = {}
const exceptionsParam = !r.style || ( r.style != 'info' && r.style != 'success' && r.style != 'danger' && r.style != 'warning');
// console.log(`${exceptionsParam} = ${!r.style} || ( ${r.style != 'info'} && ${r.style != 'success'} && ${r.style != 'danger'} && ${r.style != 'warning'})`)
r.style = exceptionsParam ? 'info' : r.style;
r.sound = !r.sound ? false : true;
if (!r.head) {
r.head = 'Hi, I\'m Notify Native — JS Library';
r.content = ''
} else {
r.head = r.head;
r.content = r.content;
};
// r.head = !r.head ? '' : r.head;
// r.content = !r.content ? '' : r.content;
if (countAlerts.length < o.maxElem) {
this.notifyCreate(r.style, r.head, r.content, r.sound);
};
};
};
/* notify.js */
/* Add + Setup the alerts */
// initialization
// const notify = new Notify({
// corner: "top_right", // Select the angle of appearance of notifications, e.g. "top_left" or "bottom_right" (default: top_right)
// delay: 5000, // Set the delay in milliseconds (default: 5000)
// maxElem: 12 // Set the maximum number of notifications on the screen (default: 10)
// });
const notify = new Notify();
// adding handlers to buttons and notification rendering
const noty_1 = document.querySelector(".btnLaunch1");
noty_1.addEventListener("click", () => {
notify.render({
head: "Info",
content: "A simple info alert—check it out!"
});
});
const noty_2 = document.querySelector(".btnLaunch2");
noty_2.addEventListener("click", () => {
notify.render({
style: "success",
head: "Success",
content: "A simple success alert—check it out!"
});
});
const noty_3 = document.querySelector(".btnLaunch3");
noty_3.addEventListener("click", () => {
notify.render({
style: "danger",
head: "Danger",
content: "A simple warning alert—check it out!",
sound: true
});
});
const noty_4 = document.querySelector(".btnLaunch4");
noty_4.addEventListener("click", () => {
notify.render({style: "warning"});
});
// setTimeout(() => {
// document.querySelector('#notifyContainer').click();
// }, 100);
// document.querySelector('#notifyContainer').addEventListener('click', function() {
// setTimeout(() => {
// notify.render({
// style: "asdavsdf",
// // head: "Hello!",
// content: "A simple success alert—check it out!"
// });
// }, 1000);
// });
/* Add the alerts */
External Link for Toast Messages and Notifications Standalone Library no JQuery - notify.js