Here's how I do it:
$.msg = function(text, style)
{
style = style || 'notice'; //<== default style if it's not set
//create message and show it
$('<div>')
.attr('class', style)
.html(text)
.insertBefore($('#page-content')) //<== where ever you want it to show
.fadeIn('slow')
.animate({opacity: 1.0}, 3000) //<== wait 3 sec before fading out
.fadeOut('slow', function()
{
$(this).remove();
});
};
Examples:
$.msg('hello world');
$.msg('it worked','success');
$.msg('there was a problem','error');
How it works
- creates a div element
- sets the style (so you can change the look)
- sets the html to show
- inserts the message where you want it
- fades in the message so it is visible
- waits 3 seconds
- fades out the message
- removes the div from the DOM -- no mess!
Bonus Sample Message Styling:
.notice, .success, .error {padding:0.8em;margin:0.77em 0.77em 0 0.77em;border-width:2px;border-style:solid;}
.notice {background-color:#FFF6BF;color:#514721;border-color:#FFD324;}
.success {background-color:#E6EFC2;color:#264409;border-color:#C6D880;}
.error {background-color:#FBE3E4;color:#8a1f11;border-color:#FBC2C4;}
.error a {color:#8a1f11;}
.notice a {color:#514721;}
.success a {color:#264409;}