firework.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Firework displays short notifications at top of page,
  3. * then fades out a few seconds later (no user interaction)
  4. * Source: https://www.jqueryscript.net/other/Simple-Top-Notification-Plugin-with-jQuery-firework-js.html
  5. * https://github.com/smalldogs/fireworkjs
  6. * @param m string message
  7. * @param t string (optional) message type ('success', 'danger')
  8. * @param l number (optional) length of time to display message in milliseconds
  9. */
  10. ;(function ($, window) {
  11. "use strict";
  12. window.firework = {
  13. launch: function(m, t, l) {
  14. if (typeof m != 'string') {
  15. console.error('Error: Call to firework() without a message');
  16. return false
  17. }
  18. var c = 'firework' // css class(es)
  19. , p = 10 // pixels from top or page to display
  20. , d = new Date()
  21. , s = d.getTime() // used to create unique element ids
  22. , fid = "firework-"+ s; // firework id
  23. if (typeof t !== 'undefined') c += ' '+ t; // add any user defined classes
  24. $('.firework').each(function(){ // account for existing fireworks and move new one below
  25. p += parseInt($(this).height()) + 30
  26. });
  27. $('<div id="'+ fid +'" class="'+ c +'">'+ m +'<a onclick="firework.remove(\'#'+ fid +'\')"><img style="height:28px;" src=close.png></a></div>')
  28. .appendTo('body')
  29. .animate({
  30. opacity: 1,
  31. top: p +'px'
  32. });
  33. setTimeout(function(){ firework.remove("#"+ fid) }, typeof l == "number" ? l : 1500);
  34. },
  35. remove : function(t) {
  36. $(t)
  37. .animate({
  38. opacity: 0
  39. })
  40. .promise()
  41. .done(function(){
  42. $(t).remove()
  43. })
  44. },
  45. sticky : function(m, t, l) {
  46. $.cookie("firework", '{ "message" : "'+ m +'", "type" : "'+ t +'", "display" : "'+ l +'" }', { path: '/' })
  47. }
  48. };
  49. // checks for firework cookie on dom ready
  50. $(function() {
  51. if (typeof $.cookie == "function") {
  52. if ($.cookie("firework")) {
  53. var ex = $.parseJSON($.cookie("firework"))
  54. setTimeout(function(){ firework.launch(ex.message, ex.type, parseInt(ex.display) > 0 ? parseInt(ex.display) : null) }, 1000)
  55. $.cookie("firework", null, { path: '/'})
  56. }
  57. }
  58. });
  59. })(jQuery, window);