Created
October 5, 2013 14:33
-
-
Save trumball/6841635 to your computer and use it in GitHub Desktop.
plugin to make it easy to add flash messages for user feedback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#status-area .flash_message { | |
padding: 5px; | |
color: green; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button class="add-item">Add to Cart</button> | |
<div id="status-area"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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