jquery.reveal.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. (function($) {
  2. /*---------------------------
  3. Defaults for Reveal
  4. ----------------------------*/
  5. /*---------------------------
  6. Listener for data-reveal-id attributes
  7. ----------------------------*/
  8. $('a[data-reveal-id]').live('click', function(e) {
  9. e.preventDefault();
  10. var modalLocation = $(this).attr('data-reveal-id');
  11. $('#'+modalLocation).reveal($(this).data());
  12. });
  13. /*---------------------------
  14. Extend and Execute
  15. ----------------------------*/
  16. $.fn.reveal = function(options) {
  17. var defaults = {
  18. animation: 'fadeAndPop', //fade, fadeAndPop, none
  19. animationspeed: 300, //how fast animtions are
  20. closeonbackgroundclick: true, //if you click background will modal close?
  21. dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal
  22. };
  23. //Extend dem' options
  24. var options = $.extend({}, defaults, options);
  25. return this.each(function() {
  26. /*---------------------------
  27. Global Variables
  28. ----------------------------*/
  29. var modal = $(this),
  30. topMeasure = parseInt(modal.css('top')),
  31. topOffset = modal.height() + topMeasure,
  32. locked = false,
  33. modalBG = $('.reveal-modal-bg');
  34. /*---------------------------
  35. Create Modal BG
  36. ----------------------------*/
  37. if(modalBG.length == 0) {
  38. modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal);
  39. }
  40. /*---------------------------
  41. Open & Close Animations
  42. ----------------------------*/
  43. //Entrance Animations
  44. modal.bind('reveal:open', function () {
  45. modalBG.unbind('click.modalEvent');
  46. $('.' + options.dismissmodalclass).unbind('click.modalEvent');
  47. if(!locked) {
  48. lockModal();
  49. if(options.animation == "fadeAndPop") {
  50. modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'});
  51. modalBG.fadeIn(options.animationspeed/2);
  52. modal.delay(options.animationspeed/2).animate({
  53. "top": $(document).scrollTop()+topMeasure + 'px',
  54. "opacity" : 1
  55. }, options.animationspeed,unlockModal());
  56. }
  57. if(options.animation == "fade") {
  58. modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure});
  59. modalBG.fadeIn(options.animationspeed/2);
  60. modal.delay(options.animationspeed/2).animate({
  61. "opacity" : 1
  62. }, options.animationspeed,unlockModal());
  63. }
  64. if(options.animation == "none") {
  65. modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure});
  66. modalBG.css({"display":"block"});
  67. unlockModal()
  68. }
  69. }
  70. modal.unbind('reveal:open');
  71. });
  72. //Closing Animation
  73. modal.bind('reveal:close', function () {
  74. if(!locked) {
  75. lockModal();
  76. if(options.animation == "fadeAndPop") {
  77. modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
  78. modal.animate({
  79. "top": $(document).scrollTop()-topOffset + 'px',
  80. "opacity" : 0
  81. }, options.animationspeed/2, function() {
  82. modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'});
  83. unlockModal();
  84. });
  85. }
  86. if(options.animation == "fade") {
  87. modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
  88. modal.animate({
  89. "opacity" : 0
  90. }, options.animationspeed, function() {
  91. modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure});
  92. unlockModal();
  93. });
  94. }
  95. if(options.animation == "none") {
  96. modal.css({'visibility' : 'hidden', 'top' : topMeasure});
  97. modalBG.css({'display' : 'none'});
  98. }
  99. }
  100. modal.unbind('reveal:close');
  101. });
  102. /*---------------------------
  103. Open and add Closing Listeners
  104. ----------------------------*/
  105. //Open Modal Immediately
  106. modal.trigger('reveal:open')
  107. //Close Modal Listeners
  108. var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent', function () {
  109. modal.trigger('reveal:close')
  110. });
  111. if(options.closeonbackgroundclick) {
  112. modalBG.css({"cursor":"pointer"})
  113. modalBG.bind('click.modalEvent', function () {
  114. modal.trigger('reveal:close')
  115. });
  116. }
  117. $('body').keyup(function(e) {
  118. if(e.which===27){ modal.trigger('reveal:close'); } // 27 is the keycode for the Escape key
  119. });
  120. /*---------------------------
  121. Animations Locks
  122. ----------------------------*/
  123. function unlockModal() {
  124. locked = false;
  125. }
  126. function lockModal() {
  127. locked = true;
  128. }
  129. });//each call
  130. }//orbit plugin call
  131. })(jQuery);