toastX is a lightweight and customizable JavaScript notification library based on jQuery. It supports different types of notifications, custom positions, animations, icons, and additional CSS classes.
Default form of toast will be shown without using any option. After clicking button, look at the bottom center of screen. Clicking button multiple times will generate same toast again. Default toast stays for 3 seconds. It supports html tags.
$.toastX('<b>Damn!</b> I am toast. I mean literally!!');
You can use duration setting to change the default timeout of 3 seconds in micro seconds(ms).
$.toastX('I am a custom duration toast set to timeout in 8 seconds.', {
duration: 8000 // ms is being used, 1000ms = 1 second
});
You can use icon setting to show an icon class defined in css. 4 basic icons are provided with the plugin. You can add more if you want to through css.
$.toastX('This is a toast with an icon', {
icon: 'success', // Adds the class 'toastx-icon-success'
});
We have created various color combinations to make lots of toast design type. Clicking the demo buttons will show various toasts with their type values.
$.toastX('I am toast of <i>type</i> <b>green</b>!', {
type: 'green'
});
You can create your own designs for toast. Let's say, if you want to create type named apple , simply create a css class toastx-apple in the included css file and then directly use it in js. Example :
.toastx-apple {
background-color: green;
}
You can define and use your own css, through additionalClass property. We have pre-defined some classes that can be used as per your need. Click below buttons for demo.
$.toastX('My borders are 4x thick!', {
additionalClass:'border-4x'
});
You can create your own custom classes for use. Let's say, if you want to create additionalClass named dashed-1x , simply create a css class dashed-1x in the included css file and then directly use it in js. Example :
.dashed-1x {
border: 1px dashed #fff;
}
You can combine these properties to create even more designs.
Although both additionalClass and type options are assigned to same element, they both can do same thing, the intention here is different. Former is for additional modification but latter is for main toast ui.
The progress bar is automatically enabled by default. You can view and choose from below demo. All predefined values are shown below
$.toastX('This toast would have striped progress bar!', {
showProgress:true, // true by default, make it false to hide progress bar
progressBarClass:'toastx-stripes-bar'
});
You can create your own progressBarClass for use in similar way as you would for a type class.
The green color of the indicator below the toast can be set as well.
$.toastX("Toast with custom progress bar color!", {
progressBarColor: "#FF5733" // Set a custom progress bar color
});
Clicking button below will show a notification in each of the possible 9 positions of screen, stating the value in the toast. Also note that multiple toasts can be shown at multiple positions.
$.toastX('This toast is displayed at bottom-center position', {
position: 'top-left' // defaults to bottom-center
});
Clicking each button will show how the animated toast will look at different positions.
$.toastX('This toast should appear and close with flipx effect.', {
animation:'flipx' // defaults to none
});
All animations are created purely with css classes. Let's understand with an example. When you apply fade animation, on creating the toast, toastx-animate-fade class is added. After 0.4 seconds, its removed so that toast becomes non animated. Similarly, just 0.4 seconds before closing the toast toastx-animate-fade-close is added which contains effects for closing state. With that being said, you can make different for creating and destroying toasts in your own css classes.
The toastX plugin provides the following lifecycle callbacks for better control:
$('#example7-button').click(function () {
$.toastX('Toast with all callbacks!', {
onBeforeCreate: function() {
alert('onBeforeCreate: Getting ready to create toast.');
},
onCreate: function() {
alert('onCreate: Toast is created and will be visible after this.');
},
onBeforeClose: function() {
alert('onBeforeClose: Toast is about to close.');
},
onClose: function() {
alert('onClose: Toast will be removed after this.');
}
, type:'warning'});
});
All animations are created purely with css classes. Let's understand with an example. When you apply fade animation, on creating the toast, toastx-animate-fade class is added. After 0.4 seconds, its removed so that toast becomes non animated. Similarly, just 0.4 seconds before closing the toast toastx-animate-fade-close is added which contains effects for closing state. With that being said, you can make different for creating and destroying toasts in your own css classes.
Sometimes, you may need to remove all toasts or a particular screen position. In that scenario, you can do following :
$.toastX.removeAll(); // deletes all toasts from all positions
$.toastX.removeAll('top-right'); // deletes all toasts only from specified position
Include the following files in your project:
<link rel="stylesheet" href="toastx/toastx.css">
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="toastx/toast.js"></script>
Just like any jQuery plugin, you can use it inside functions or events
$('#example').click(function () { // on clicking id = example
$.toastX('This toast example includes almost all basic settings.', {
type:'disco', // defaults to default
animation:'flipx', // defaults to none
duration:5000, // defaults to 3000
position:'top-center', // defaults to bottom-center
progressBarClass:'toastx-stripes-bar', // defaults to none
additionalClass:'movebg', // defaults to none
showProgress:true, // true by default
barColor:'#FF51B5' // progress bar color, defaults to #99FF00
});
});
Option | Description | Default values |
---|---|---|
type |
Type of toast notification. Options: success , error , info , warning . |
None |
position |
Position of the toast. Options: top-left , top-right , bottom-left , bottom-right , top-center , bottom-center , center-center . |
bottom-center |
duration |
Time (in milliseconds) before the toast disappears. | 3000 |
icon |
URL or CSS class for the toast icon. | None |
animation |
Animation type for showing and hiding the toast. Options: fade , slide , reveal , wiggle , blur , zoom , flipx , flipy , flipz , flipxz , flipyz , circle , none . |
none |
additionalClass |
Additional CSS class to style the toast. | None |
progressBarClass |
Additional CSS class to style the progress bar. Options : toast-stripes-bar , toast-blinking-bar , toast-stripes-bar moving |
None |
barColor |
Basic color of progress bar! | #99FF00 |
showProgress |
Whether to show progress bar or hide. Boolean. Options : true , false |
true |
onBeforeCreate |
Execute before the toast is created. | None |
onCreate |
Execute immediately after the toast appears. | None |
onBeforeClose |
Execute before the toast starts closing, like prompting the user. | None |
onClose |
Execute after the toast is completely removed, such as cleanup tasks. | None |
$.toastX.removeAll(); |
Removes toasts from specified position or from all positions. Refer example 8. | None |
You have reached the end of toastX documentation page. If you have more questions you can reach me via phploaded's contact page. No gurantees but i will try to look into it as soon as possible.