waitMe.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. waitMe - 1.15 [15.02.16]
  3. Author: vadimsva
  4. Github: https://github.com/vadimsva/waitMe
  5. */
  6. (function($) {
  7. $.fn.waitMe = function(method) {
  8. return this.each(function() {
  9. var elem = $(this),
  10. elemClass = 'waitMe',
  11. waitMe_text,
  12. effectObj,
  13. effectElemCount,
  14. createSubElem = false,
  15. specificAttr = 'background-color',
  16. addStyle = '',
  17. effectElemHTML = '',
  18. waitMeObj,
  19. _options,
  20. currentID;
  21. var methods = {
  22. init : function() {
  23. var _defaults = {
  24. effect: 'bounce',
  25. text: '',
  26. bg: 'rgba(255,255,255,0.7)',
  27. color: '#000',
  28. maxSize: '',
  29. source: '',
  30. onClose: function() {}
  31. };
  32. _options = $.extend(_defaults, method);
  33. currentID = new Date().getMilliseconds();
  34. waitMeObj = $('<div class="' + elemClass + '" data-waitme_id="' + currentID + '"></div>');
  35. switch (_options.effect) {
  36. case 'none':
  37. effectElemCount = 0;
  38. break;
  39. case 'bounce':
  40. effectElemCount = 3;
  41. break;
  42. case 'rotateplane':
  43. effectElemCount = 1;
  44. break;
  45. case 'stretch':
  46. effectElemCount = 5;
  47. break;
  48. case 'orbit':
  49. effectElemCount = 2;
  50. break;
  51. case 'roundBounce':
  52. effectElemCount = 12;
  53. break;
  54. case 'win8':
  55. effectElemCount = 5;
  56. createSubElem = true;
  57. break;
  58. case 'win8_linear':
  59. effectElemCount = 5;
  60. createSubElem = true;
  61. break;
  62. case 'ios':
  63. effectElemCount = 12;
  64. break;
  65. case 'facebook':
  66. effectElemCount = 3;
  67. break;
  68. case 'rotation':
  69. effectElemCount = 1;
  70. specificAttr = 'border-color';
  71. break;
  72. case 'timer':
  73. effectElemCount = 2;
  74. if ($.isArray(_options.color)) {
  75. var color = _options.color[0];
  76. } else {
  77. var color = _options.color;
  78. }
  79. addStyle = 'border-color:' + color;
  80. break;
  81. case 'pulse':
  82. effectElemCount = 1;
  83. specificAttr = 'border-color';
  84. break;
  85. case 'progressBar':
  86. effectElemCount = 1;
  87. break;
  88. case 'bouncePulse':
  89. effectElemCount = 3;
  90. break;
  91. case 'img':
  92. effectElemCount = 1;
  93. break;
  94. }
  95. if (addStyle !== '') {
  96. addStyle += ';';
  97. }
  98. if (effectElemCount > 0) {
  99. if(_options.effect === 'img') {
  100. effectElemHTML = '<img src="' + _options.source + '">';
  101. } else {
  102. for (var i = 1; i <= effectElemCount; ++i) {
  103. if ($.isArray(_options.color)) {
  104. var color = _options.color[i];
  105. if (color == undefined) {
  106. color = '#000';
  107. }
  108. } else {
  109. var color = _options.color;
  110. }
  111. if (createSubElem) {
  112. effectElemHTML += '<div class="' + elemClass + '_progress_elem' + i + '"><div style="' + specificAttr +':' + color + '"></div></div>';
  113. } else {
  114. effectElemHTML += '<div class="' + elemClass + '_progress_elem' + i + '" style="' + specificAttr + ':' + color + '"></div>';
  115. }
  116. }
  117. }
  118. effectObj = $('<div class="' + elemClass + '_progress ' + _options.effect + '" style="' + addStyle + '">' + effectElemHTML + '</div>');
  119. }
  120. if (_options.text && _options.maxSize === '') {
  121. if ($.isArray(_options.color)) {
  122. var color = _options.color[0];
  123. } else {
  124. var color = _options.color;
  125. }
  126. waitMe_text = $('<div class="' + elemClass + '_text" style="color:' + color + '">' + _options.text + '</div>');
  127. }
  128. var elemObj = elem.find('> .' + elemClass);
  129. if (elemObj) {
  130. elemObj.remove();
  131. }
  132. var waitMeDivObj = $('<div class="' + elemClass + '_content"></div>');
  133. waitMeDivObj.append(effectObj, waitMe_text);
  134. waitMeObj.append(waitMeDivObj);
  135. if (elem[0].tagName == 'HTML') {
  136. elem = $('body');
  137. }
  138. elem.addClass(elemClass + '_container').attr('data-waitme_id', currentID).append(waitMeObj);
  139. elemObj = elem.find('> .' + elemClass);
  140. var elemContentObj = elem.find('.' + elemClass + '_content');
  141. elemObj.css({background: _options.bg});
  142. elemContentObj.css({marginTop: - elemContentObj.outerHeight() / 2 + 'px'});
  143. if (_options.maxSize !== '') {
  144. var elemH = effectObj.outerHeight();
  145. var elemW = effectObj.outerWidth();
  146. var elemMax = elemH;
  147. if (_options.effect === 'img') {
  148. effectObj.css({height: _options.maxSize + 'px'});
  149. effectObj.find('>img').css({maxHeight: '100%'});
  150. elemContentObj.css({marginTop: - elemContentObj.outerHeight() / 2 + 'px'});
  151. } else {
  152. if (_options.maxSize < elemMax) {
  153. waitMeDivObj.css({transform: 'scale(' + _options.maxSize / elemMax + ')'});
  154. }
  155. }
  156. }
  157. function setElTop(getTop) {
  158. elemContentObj.css({top: 'auto', transform: 'translateY(' + getTop + 'px) translateZ(0)'});
  159. }
  160. if (elem.outerHeight() > $(window).height()) {
  161. var sTop = $(window).scrollTop(),
  162. elH = elemContentObj.outerHeight(),
  163. elTop = elem.offset().top,
  164. cH = elem.outerHeight(),
  165. getTop = sTop - elTop + $(window).height()/2;
  166. if (getTop < 0) {
  167. getTop = Math.abs(getTop);
  168. }
  169. if (getTop - elH >= 0 && getTop + elH <= cH) {
  170. if (elTop - sTop > $(window).height()/2) {
  171. getTop = elH;
  172. }
  173. setElTop(getTop);
  174. } else {
  175. if (sTop > elTop + cH - elH) {
  176. getTop = sTop - elTop - elH;
  177. } else {
  178. getTop = sTop - elTop + elH;
  179. }
  180. setElTop(getTop);
  181. }
  182. $(document).scroll(function() {
  183. var sTop = $(window).scrollTop(),
  184. getTop = sTop - elTop + $(window).height()/2;
  185. if (getTop - elH >= 0 && getTop + elH <= cH) {
  186. setElTop(getTop);
  187. }
  188. });
  189. }
  190. elemObj.on('destroyed', function() {
  191. if (_options.onClose && $.isFunction(_options.onClose)) {
  192. _options.onClose();
  193. }
  194. elemObj.trigger('close');
  195. });
  196. $.event.special.destroyed = {
  197. remove: function(o) {
  198. if (o.handler) {
  199. o.handler();
  200. }
  201. }
  202. };
  203. return elemObj;
  204. },
  205. hide : function() {
  206. waitMeClose();
  207. }
  208. };
  209. function waitMeClose() {
  210. var currentID = elem.attr('data-waitme_id');
  211. elem.removeClass(elemClass + '_container').removeAttr('data-waitme_id');
  212. elem.find('.' + elemClass + '[data-waitme_id="' + currentID + '"]').remove();
  213. }
  214. if (methods[method]) {
  215. return methods[method].apply( this, Array.prototype.slice.call(arguments, 1));
  216. } else if (typeof method === 'object' || ! method) {
  217. return methods.init.apply(this, arguments);
  218. }
  219. });
  220. };
  221. $(window).on('load', function(){
  222. $('body.waitMe_body').addClass('hideMe');
  223. setTimeout(function(){
  224. $('body.waitMe_body').find('.waitMe_container:not([data-waitme_id])').remove();
  225. $('body.waitMe_body').removeClass('waitMe_body hideMe');
  226. },200);
  227. });
  228. })(jQuery);