Skip to content

Instantly share code, notes, and snippets.

@stephencozart
Created May 11, 2012 06:17
Show Gist options
  • Save stephencozart/2657885 to your computer and use it in GitHub Desktop.
Save stephencozart/2657885 to your computer and use it in GitHub Desktop.
jQuery Flash Helper
/**
* Usage - Review the defaults for additional options that can be set
*
* Easily add alert ui messages to the dom without messing with the dom!
*
* $.flash({content: 'record saved...',auto_remove: false});
*/
$.extend({
flash: function(options) {
defaults = {
target: $('body'),
content: 'my flash message',
alert_class: 'success',
duration: 3000,
speed: 2000,
position: null,
left: null,
right: null,
top: null,
bottom: null,
auto_remove: true,
show_close: true,
insert_mode: 'append'
}
var o = $.extend(defaults,options);
var _wrapper = $('<div class="js-alert"/>');
var remove = function(force) {
if(o.auto_remove === true || force === true)
{
_wrapper.fadeOut(o.speed,function() {
_wrapper.remove();
});
}
};
_wrapper.css('display','none');
if(o.position !== null)
_wrapper.css('position',o.position);
if(o.left !== null)
_wrapper.css('left',o.left);
if(o.right !== null)
_wrapper.css('right',o.right);
if(o.bottom !== null)
_wrapper.css('bottom',o.bottom);
if(o.top !== null)
_wrapper.css('top',o.top);
var _content = $('<div/>');
_content.addClass(o.alert_class)
.addClass('alert')
.html(o.content);
if(o.auto_remove === false && o.show_close === true)
{
var _closer = $('<span class="js-alert-close">X</span>');
_closer.click(function() {
remove(true);
});
_content.append(_closer);
}
if(o.insert_mode == 'prepend')
o.target.prepend(_wrapper.html(_content));
else
o.target.append(_wrapper.html(_content));
_wrapper.fadeIn(o.speed,function() {
_wrapper.animate({opacity: 1},o.duration,function() {
remove();
});
});
return _wrapper;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment