jquery.goup.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. *
  3. * Copyright (c) 2014 Daniele Lenares (https://github.com/Ryuk87)
  4. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  5. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  6. *
  7. * Version 1.0.0
  8. *
  9. */
  10. (function ( $ ) {
  11. $.goup = function(user_params) {
  12. /* Default Params */
  13. var params = $.extend({
  14. location : 'right',
  15. locationOffset : 20,
  16. bottomOffset : 10,
  17. containerSize : 40,
  18. containerRadius : 10,
  19. containerClass : 'goup-container',
  20. arrowClass : 'goup-arrow',
  21. alwaysVisible : false,
  22. trigger: 500,
  23. entryAnimation : 'fade',
  24. goupSpeed : 'slow',
  25. hideUnderWidth : 500,
  26. containerColor : '#000',
  27. arrowColor : '#fff',
  28. title : '',
  29. titleAsText : false,
  30. titleAsTextClass : 'goup-text'
  31. }, user_params);
  32. /* */
  33. $('body').append('<div style="display:none" class="'+params.containerClass+'"></div>');
  34. var container = $('.'+params.containerClass);
  35. $(container).html('<div class="'+params.arrowClass+'"></div>');
  36. var arrow = $('.'+params.arrowClass);
  37. /* Parameters check */
  38. if (params.location != 'right' && params.location != 'left') {
  39. params.location = 'right';
  40. }
  41. if (params.locationOffset < 0) {
  42. params.locationOffset = 0;
  43. }
  44. if (params.bottomOffset < 0) {
  45. params.bottomOffset = 0;
  46. }
  47. if (params.containerSize < 20) {
  48. params.containerSize = 20;
  49. }
  50. if (params.containerRadius < 0) {
  51. params.containerRadius = 0;
  52. }
  53. if (params.trigger < 0) {
  54. params.trigger = 0;
  55. }
  56. if (params.hideUnderWidth < 0) {
  57. params.hideUnderWidth = 0;
  58. }
  59. var checkColor = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
  60. if (!checkColor.test(params.containerColor)) {
  61. params.containerColor = '#000';
  62. }
  63. if (!checkColor.test(params.arrowColor)) {
  64. params.arrowColor = '#fff';
  65. }
  66. if (params.title === '') {
  67. params.titleAsText = false;
  68. }
  69. /* */
  70. /* Container Style */
  71. var containerStyle = {};
  72. containerStyle = {
  73. position : 'fixed',
  74. width : params.containerSize,
  75. height : params.containerSize,
  76. background : params.containerColor,
  77. cursor: 'pointer'
  78. };
  79. containerStyle['bottom'] = params.bottomOffset;
  80. containerStyle[params.location] = params.locationOffset;
  81. containerStyle['border-radius'] = params.containerRadius;
  82. $(container).css(containerStyle);
  83. if (!params.titleAsText) {
  84. $(container).attr('title', params.title);
  85. } else {
  86. $('body').append('<div class="'+params.titleAsTextClass+'">'+params.title+'</div>');
  87. var textContainer = $('.'+params.titleAsTextClass);
  88. $(textContainer).attr('style', $(container).attr('style'));
  89. $(textContainer).css('background', 'transparent')
  90. .css('width', params.containerSize + 40)
  91. .css('height', 'auto')
  92. .css('text-align', 'center')
  93. .css(params.location, params.locationOffset - 20);
  94. var containerNewBottom = $(textContainer).height() + 10;
  95. $(container).css('bottom', '+='+containerNewBottom+'px');
  96. }
  97. /* Arrow Style */
  98. var arrowStyle = {};
  99. var borderSize = 0.25 * params.containerSize;
  100. arrowStyle = {
  101. width : 0,
  102. height : 0,
  103. margin : '0 auto',
  104. 'padding-top' : Math.ceil(0.325 * params.containerSize),
  105. 'border-style' : 'solid',
  106. 'border-width' : '0 '+borderSize+'px '+borderSize+'px '+borderSize+'px',
  107. 'border-color' : 'transparent transparent '+params.arrowColor+' transparent'
  108. };
  109. $(arrow).css(arrowStyle);
  110. /* */
  111. /* params.trigger Hide under a certain width */
  112. var isHidden = false;
  113. $(window).resize(function(){
  114. if ($(window).outerWidth() <= params.hideUnderWidth) {
  115. isHidden = true;
  116. do_animation($(container), 'hide', params.entryAnimation);
  117. if (textContainer)
  118. do_animation($(textContainer), 'hide', params.entryAnimation);
  119. } else {
  120. isHidden = false;
  121. $(window).trigger('scroll');
  122. }
  123. });
  124. /* If i load the page under a certain width, i don't have the event 'resize' */
  125. if ($(window).outerWidth() <= params.hideUnderWidth) {
  126. isHidden = true;
  127. $(container).hide();
  128. if (textContainer)
  129. $(textContainer).hide();
  130. }
  131. /* params.trigger show event */
  132. if (!params.alwaysVisible) {
  133. $(window).scroll(function(){
  134. if ($(window).scrollTop() >= params.trigger && !isHidden) {
  135. do_animation($(container), 'show', params.entryAnimation);
  136. if (textContainer)
  137. do_animation($(textContainer), 'show', params.entryAnimation);
  138. }
  139. if ($(window).scrollTop() < params.trigger && !isHidden) {
  140. do_animation($(container), 'hide', params.entryAnimation);
  141. if (textContainer)
  142. do_animation($(textContainer), 'hide', params.entryAnimation);
  143. }
  144. });
  145. } else {
  146. do_animation($(container), 'show', params.entryAnimation);
  147. if (textContainer)
  148. do_animation($(textContainer), 'show', params.entryAnimation);
  149. }
  150. /* If i load the page and the scroll is over the trigger, i don't have immediately the event 'scroll' */
  151. if ($(window).scrollTop() >= params.trigger && !isHidden) {
  152. do_animation($(container), 'show', params.entryAnimation);
  153. if (textContainer)
  154. do_animation($(textContainer), 'show', params.entryAnimation);
  155. }
  156. /* Click event */
  157. var notClicked = true;
  158. $(container).add(textContainer).on('click', function(){
  159. if (notClicked) {
  160. notClicked = false;
  161. $('html,body').animate({ scrollTop: 0 }, params.goupSpeed, function(){ notClicked = true });
  162. }
  163. return false;
  164. });
  165. };
  166. /* Private function for the animation */
  167. function do_animation(obj, type, animation) {
  168. if (type == 'show') {
  169. switch(animation) {
  170. case 'fade':
  171. obj.fadeIn();
  172. break;
  173. case 'slide':
  174. obj.slideDown();
  175. break;
  176. default:
  177. obj.fadeIn();
  178. }
  179. } else {
  180. switch(animation) {
  181. case 'fade':
  182. obj.fadeOut();
  183. break;
  184. case 'slide':
  185. obj.slideUp();
  186. break;
  187. default:
  188. obj.fadeOut();
  189. }
  190. }
  191. }
  192. }( jQuery ));