sticky.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Sticky v1.0 by Daniel Raftery
  2. // http://thrivingkings.com/sticky
  3. //
  4. // http://twitter.com/ThrivingKings
  5. (function( $ )
  6. {
  7. // Using it without an object
  8. $.sticky = function(note, options, callback) { return $.fn.sticky(note, options, callback); };
  9. $.fn.sticky = function(note, options, callback)
  10. {
  11. // Default settings
  12. var position = 'top-right'; // top-left, top-right, bottom-left, or bottom-right
  13. var settings =
  14. {
  15. 'speed' : 'fast', // animations: fast, slow, or integer
  16. 'duplicates' : true, // true or false
  17. 'autoclose' : 5000 // integer or false
  18. };
  19. // Passing in the object instead of specifying a note
  20. if(!note)
  21. { note = this.html(); }
  22. if(options)
  23. { $.extend(settings, options); }
  24. // Variables
  25. var display = true;
  26. var duplicate = 'no';
  27. // Somewhat of a unique ID
  28. var uniqID = Math.floor(Math.random()*99999);
  29. // Handling duplicate notes and IDs
  30. $('.sticky-note').each(function()
  31. {
  32. if($(this).html() == note && $(this).is(':visible'))
  33. {
  34. duplicate = 'yes';
  35. if(!settings['duplicates'])
  36. { display = false; }
  37. }
  38. if($(this).attr('id')==uniqID)
  39. { uniqID = Math.floor(Math.random()*9999999); }
  40. });
  41. // Make sure the sticky queue exists
  42. if(!$('body').find('.sticky-queue').html())
  43. { $('body').append('<div class="sticky-queue ' + position + '"></div>'); }
  44. // Can it be displayed?
  45. if(display)
  46. {
  47. // Building and inserting sticky note
  48. $('.sticky-queue').prepend('<div class="sticky border-' + position + '" id="' + uniqID + '"></div>');
  49. $('#' + uniqID).append('<img src="modules/jsc/sticky/close.png" class="sticky-close" rel="' + uniqID + '" title="Close" />');
  50. $('#' + uniqID).append('<div class="sticky-note" rel="' + uniqID + '">' + note + '</div>');
  51. // Smoother animation
  52. var height = $('#' + uniqID).height();
  53. $('#' + uniqID).css('height', height);
  54. $('#' + uniqID).slideDown(settings['speed']);
  55. display = true;
  56. }
  57. // Listeners
  58. $('.sticky').ready(function()
  59. {
  60. // If 'autoclose' is enabled, set a timer to close the sticky
  61. if(settings['autoclose'])
  62. { $('#' + uniqID).delay(settings['autoclose']).fadeOut(settings['speed']); }
  63. });
  64. // Closing a sticky
  65. $('.sticky-close').click(function()
  66. { $('#' + $(this).attr('rel')).dequeue().fadeOut(settings['speed']); });
  67. // Callback data
  68. var response =
  69. {
  70. 'id' : uniqID,
  71. 'duplicate' : duplicate,
  72. 'displayed' : display,
  73. 'position' : position
  74. }
  75. // Callback function?
  76. if(callback)
  77. { callback(response); }
  78. else
  79. { return(response); }
  80. }
  81. })( jQuery );