Skip to content

Instantly share code, notes, and snippets.

@trumball
Created October 5, 2013 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trumball/6841635 to your computer and use it in GitHub Desktop.
Save trumball/6841635 to your computer and use it in GitHub Desktop.
plugin to make it easy to add flash messages for user feedback
#status-area .flash_message {
padding: 5px;
color: green;
}
<button class="add-item">Add to Cart</button>
<div id="status-area"></div>
(function($) {
$.fn.flash_message = function(options) {
options = $.extend({
text: 'Done',
time: 1000,
how: 'before',
class_name: ''
}, options);
return $(this).each(function() {
if( $(this).parent().find('.flash_message').get(0) )
return;
var message = $('<span />', {
'class': 'flash_message ' + options.class_name,
text: options.text
}).hide().fadeIn('fast');
$(this)[options.how](message);
message.delay(options.time).fadeOut('normal', function() {
$(this).remove();
});
});
};
})(jQuery);
//Usage Examples
$('#status-area').flash_message({
text: 'Added to cart!', // text to show
how: 'append', // ['append', 'prepend', 'before', 'after']
time: 1000, // how long should the message be visible
class_name: 'success' // additional class names for CSS hooks
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment