(function ($) {
//
// $.fn.firework
//
// Firework plugin
//
// Author: Antoine BERNIER (abernier)
//
var settings;
function firework(el, options) {
var $stars = $();
//
// Generate stars markup (within a container)
//
$(el)
.css('position', function (index, value) {
return value === 'static' ? 'relative' : false;
})
.append($('').css({
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom:0
})
.append(function () {
var i = options.amount;
while (i--) {
$stars = $stars.add(($('', {
"class": options.klass,
html: ($.isArray(options.content) ? options.content[random(0, options.content.length - 1)] : options.content)
}).css({
position: 'absolute',
left: '50%',
top: '50%'
})));
}
return $stars;
})
)
;
// Once stars into the DOM, use their width/height to apply the negative margin offset
$stars.each(function (i, el) {
$(el).css('margin-left', function (index, value) {
return -($(this).width() / 2);
}).css('margin-top', function (index, value) {
return -($(this).height() / 2);
});
});
//
// Animate stars
//
(function () {
var animations = [];
$stars.each(function (i, el) {
animations.push($(el).animate({
left: minusOrPlus() + '=' + options.boundary * random(0, 50) + '%',
top: minusOrPlus() + '=' + options.boundary * random(0, 50) + '%',
fontSize: random(100, 100 * options.boundary) + '%',
opacity: 0
}, random(~~(options.duration / options.durationVariation), options.duration), options.easing));
});
$.when.apply(this, animations).done(function () {
$stars.parent().remove();
});
}());
//
// Helpers
//
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function minusOrPlus() {
return Math.random() > .5 ? '+' : '-';
}
}
// Default settings
settings = {
amount: 50,
boundary: 1.5,
content: '★',
klass: 'firework star',
duration: 3000,
durationVariation: 3,
easing: 'swing'
};
$.fn.firework = function (options) {
return this.each(function (i, el) {
firework(el, $.extend({}, settings, options));
});
};
}(jQuery));