// requires jQuery ... for now
function ToastBuilder(options) {
// options are optional
var opts = options || {};
// setup some defaults
opts.defaultText = opts.defaultText || 'default text';
opts.displayTime = opts.displayTime || 3000;
opts.target = opts.target || 'body';
return function (text) {
$('<div/>')
.addClass('toast')
.prependTo($(opts.target))
.text(text || opts.defaultText)
.queue(function(next) {
$(this).css({
'opacity': 1
});
var topOffset = 15;
$('.toast').each(function() {
var $this = $(this);
var height = $this.outerHeight();
var offset = 15;
$this.css('top', topOffset + 'px');
topOffset += height + offset;
});
next();
})
.delay(opts.displayTime)
.queue(function(next) {
var $this = $(this);
var width = $this.outerWidth() + 20;
$this.css({
'right': '-' + width + 'px',
'opacity': 0
});
next();
})
.delay(600)
.queue(function(next) {
$(this).remove();
next();
});
};
}
// customize it with your own options
var myOptions = {
defaultText: 'Toast, yo!',
displayTime: 3000,
target: 'body'
};
//position: 'top right', /* TODO: make this */
//bgColor: 'rgba(0,0,0,0.5)', /* TODO: make this */
// to get it started, instantiate a copy of
// ToastBuilder passing our custom options
var showtoast = new ToastBuilder(myOptions);
// now you can fire off a toast just calling
// our new instance passing a string, like this:
// showtoast('hello, world!');
// wire it up
$('#toast-btn').click(function() {
showtoast($('#textbox').val());
});
// fires off a toast when you hit enter
$('#textbox').keypress(function(e) {
if (e.which == 13) {
showtoast($('#textbox').val());
return false;
}
});
// Demo:
$('body')
.queue(function(next) {
showtoast('Hey look, toast!');
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.