"How To Create A Toast Notification in Javascript"
Bootstrap 4.1.1 Snippet by muhittinbudak

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<title>Toast Notification In Javascript</title>
<link rel="stylesheet" href="style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Font Awesome icons -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css"
/>
<script src="script.js" defer></script>
</head>
<body>
<ul class="notifications"></ul>
<div class="buttons">
<button class="btn" id="error">Error</button>
<button class="btn" id="warning">Warning</button>
<button class="btn" id="info">Info</button>
<button class="btn" id="success">Success</button>
<button class="btn" id="random">Random</button>
</div>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Import Google font - Poppins */
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
:root {
--dark: #78fad1;
--light: #ffffff;
--success: #0abf30;
--error: #e24d4c;
--warning: #e9bd0c;
--info: #3498db;
--random: #eb43ff;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: var(--dark);
}
.notifications {
position: fixed;
top: 30px;
right: 20px;
}
.notifications :where(.toast, .column) {
display: flex;
align-items: center;
}
.notifications .toast {
width: 400px;
position: relative;
overflow: hidden;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const notifications = document.querySelector(".notifications"),
buttons = document.querySelectorAll(".buttons .btn")
const toastDetails = {
timer: 5000,
success: {
icon: "fa-circle-check",
text: "Hello World: This is a success toast.",
},
error: {
icon: "fa-circle-xmark",
text: "Hello World: This is an error toast.",
},
warning: {
icon: "fa-triangle-exclamation",
text: "Hello World: This is a warning toast.",
},
info: {
icon: "fa-circle-info",
text: "Hello World: This is an information toast.",
},
random: {
icon: "fa-solid fa-star",
text: "Hello World: This is a random toast.",
},
}
const removeToast = (toast) => {
toast.classList.add("hide")
if (toast.timeoutId) clearTimeout(toast.timeoutId)
setTimeout(() => toast.remove(), 500)
}
const createToast = (id) => {
const { icon, text } = toastDetails[id]
const toast = document.createElement("li")
toast.className = `toast ${id}`
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments: