I have a simple toast notification code that I've collected from W3 Schools. But there's a problem. It use id, and we know that an id can be use for once. I want to change id and use it as class. So that I can use for many time. If it can be done by jquery, that will be also easy.
Another fact is that, I want to add a close button. So that, anyone can close it. As it disappear after 3 seconds, so there will be two option. Auto remove / fade out and closable system.
Look at my code:
`
<style type="text/css">
#snackbar {
visibility: hidden;
min-width: 250px;
margin-left: -125px;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 2px;
padding: 16px;
position: fixed;
z-index: 1;
left: 50%;
bottom: 30px;
}
#snackbar.show {
visibility: visible;
-webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
animation: fadein 0.5s, fadeout 0.5s 2.5s;
}
@-webkit-keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@-webkit-keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
@keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
</style>
<button onclick="myFunction()">Show Snackbar</button>
<div id="snackbar">Some text some message..</div>
<script type="text/javascript">
function myFunction() {
var x = document.getElementById("snackbar");
x.className = "show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
</script>
`
How can I do it ?
Change id to class and add a close button to toast notification.
CodePudding user response:
In css #
is used as a prefix to select ids and .
prefix is used to select based on class.
- Modify your HTML and use
class
name for toast divs - Modify your CSS to specify rules for the
class
you used - Modify the JavaScript code to use
document.getElementsByClassName
instead ofdocument.getElementById(..)
- Further modify the JavaScript code to handle the returned element(s) in a loop as this method returns a
HTMLCollection
(Click Here for API Docs).
Here is a quick jsFiddle I created with two toasts - https://jsfiddle.net/6hq2vzwu/
EDIT: Another (updated) version of the fiddle - you can click on the fist toast to close - https://jsfiddle.net/r9npkgc8/3/
CodePudding user response:
function myFunction() {
var x = document.querySelector(".snackbar");
x.className = "snackbar show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
.snackbar {
visibility: hidden;
min-width: 250px;
margin-left: -125px;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 2px;
padding: 16px;
position: fixed;
z-index: 1;
left: 50%;
bottom: 30px;
}
.snackbar.show {
visibility: visible;
-webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
animation: fadein 0.5s, fadeout 0.5s 2.5s;
}
@-webkit-keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@-webkit-keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
@keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
<button onclick="myFunction()">Show Snackbar</button>
<div >Some text some message..</div>