You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
9.8 KiB

2 years ago
  1. // Sticky Plugin v1.0.4 for jQuery
  2. // =============
  3. // Author: Anthony Garand
  4. // Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk)
  5. // Improvements by Leonardo C. Daronco (daronco)
  6. // Created: 02/14/2011
  7. // Date: 07/20/2015
  8. // Website: http://stickyjs.com/
  9. // Description: Makes an element on the page stick on the screen as you scroll
  10. // It will only set the 'top' and 'position' of your element, you
  11. // might need to adjust the width in some cases.
  12. (function (factory) {
  13. if (typeof define === 'function' && define.amd) {
  14. // AMD. Register as an anonymous module.
  15. define(['jquery'], factory);
  16. } else if (typeof module === 'object' && module.exports) {
  17. // Node/CommonJS
  18. module.exports = factory(require('jquery'));
  19. } else {
  20. // Browser globals
  21. factory(jQuery);
  22. }
  23. }(function ($) {
  24. var slice = Array.prototype.slice; // save ref to original slice()
  25. var splice = Array.prototype.splice; // save ref to original slice()
  26. var defaults = {
  27. topSpacing: 0,
  28. bottomSpacing: 0,
  29. className: 'is-sticky',
  30. wrapperClassName: 'sticky-wrapper',
  31. center: false,
  32. getWidthFrom: '',
  33. widthFromWrapper: true, // works only when .getWidthFrom is empty
  34. responsiveWidth: false,
  35. zIndex: 'inherit'
  36. },
  37. $window = $(window),
  38. $document = $(document),
  39. sticked = [],
  40. windowHeight = $window.height(),
  41. scroller = function() {
  42. var scrollTop = $window.scrollTop(),
  43. documentHeight = $document.height(),
  44. dwh = documentHeight - windowHeight,
  45. extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
  46. for (var i = 0, l = sticked.length; i < l; i++) {
  47. var s = sticked[i],
  48. elementTop = s.stickyWrapper.offset().top,
  49. etse = elementTop - s.topSpacing - extra;
  50. //update height in case of dynamic content
  51. s.stickyWrapper.css('height', s.stickyElement.outerHeight());
  52. if (scrollTop <= etse) {
  53. if (s.currentTop !== null) {
  54. s.stickyElement
  55. .css({
  56. 'width': '',
  57. 'position': '',
  58. 'top': '',
  59. 'z-index': ''
  60. });
  61. s.stickyElement.parent().removeClass(s.className);
  62. s.stickyElement.trigger('sticky-end', [s]);
  63. s.currentTop = null;
  64. }
  65. }
  66. else {
  67. var newTop = documentHeight - s.stickyElement.outerHeight()
  68. - s.topSpacing - s.bottomSpacing - scrollTop - extra;
  69. if (newTop < 0) {
  70. newTop = newTop + s.topSpacing;
  71. } else {
  72. newTop = s.topSpacing;
  73. }
  74. if (s.currentTop !== newTop) {
  75. var newWidth;
  76. if (s.getWidthFrom) {
  77. padding = s.stickyElement.innerWidth() - s.stickyElement.width();
  78. newWidth = $(s.getWidthFrom).width() - padding || null;
  79. } else if (s.widthFromWrapper) {
  80. newWidth = s.stickyWrapper.width();
  81. }
  82. if (newWidth == null) {
  83. newWidth = s.stickyElement.width();
  84. }
  85. s.stickyElement
  86. .css('width', newWidth)
  87. .css('position', 'fixed')
  88. .css('top', newTop)
  89. .css('z-index', s.zIndex);
  90. s.stickyElement.parent().addClass(s.className);
  91. if (s.currentTop === null) {
  92. s.stickyElement.trigger('sticky-start', [s]);
  93. } else {
  94. // sticky is started but it have to be repositioned
  95. s.stickyElement.trigger('sticky-update', [s]);
  96. }
  97. if (s.currentTop === s.topSpacing && s.currentTop > newTop || s.currentTop === null && newTop < s.topSpacing) {
  98. // just reached bottom || just started to stick but bottom is already reached
  99. s.stickyElement.trigger('sticky-bottom-reached', [s]);
  100. } else if(s.currentTop !== null && newTop === s.topSpacing && s.currentTop < newTop) {
  101. // sticky is started && sticked at topSpacing && overflowing from top just finished
  102. s.stickyElement.trigger('sticky-bottom-unreached', [s]);
  103. }
  104. s.currentTop = newTop;
  105. }
  106. // Check if sticky has reached end of container and stop sticking
  107. var stickyWrapperContainer = s.stickyWrapper.parent();
  108. var unstick = (s.stickyElement.offset().top + s.stickyElement.outerHeight() >= stickyWrapperContainer.offset().top + stickyWrapperContainer.outerHeight()) && (s.stickyElement.offset().top <= s.topSpacing);
  109. if( unstick ) {
  110. s.stickyElement
  111. .css('position', 'absolute')
  112. .css('top', '')
  113. .css('bottom', 0)
  114. .css('z-index', '');
  115. } else {
  116. s.stickyElement
  117. .css('position', 'fixed')
  118. .css('top', newTop)
  119. .css('bottom', '')
  120. .css('z-index', s.zIndex);
  121. }
  122. }
  123. }
  124. },
  125. resizer = function() {
  126. windowHeight = $window.height();
  127. for (var i = 0, l = sticked.length; i < l; i++) {
  128. var s = sticked[i];
  129. var newWidth = null;
  130. if (s.getWidthFrom) {
  131. if (s.responsiveWidth) {
  132. newWidth = $(s.getWidthFrom).width();
  133. }
  134. } else if(s.widthFromWrapper) {
  135. newWidth = s.stickyWrapper.width();
  136. }
  137. if (newWidth != null) {
  138. s.stickyElement.css('width', newWidth);
  139. }
  140. }
  141. },
  142. methods = {
  143. init: function(options) {
  144. return this.each(function() {
  145. var o = $.extend({}, defaults, options);
  146. var stickyElement = $(this);
  147. var stickyId = stickyElement.attr('id');
  148. var wrapperId = stickyId ? stickyId + '-' + defaults.wrapperClassName : defaults.wrapperClassName;
  149. var wrapper = $('<div></div>')
  150. .attr('id', wrapperId)
  151. .addClass(o.wrapperClassName);
  152. stickyElement.wrapAll(function() {
  153. if ($(this).parent("#" + wrapperId).length == 0) {
  154. return wrapper;
  155. }
  156. });
  157. var stickyWrapper = stickyElement.parent();
  158. if (o.center) {
  159. stickyWrapper.css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"});
  160. }
  161. if (stickyElement.css("float") === "right") {
  162. stickyElement.css({"float":"none"}).parent().css({"float":"right"});
  163. }
  164. o.stickyElement = stickyElement;
  165. o.stickyWrapper = stickyWrapper;
  166. o.currentTop = null;
  167. sticked.push(o);
  168. methods.setWrapperHeight(this);
  169. methods.setupChangeListeners(this);
  170. });
  171. },
  172. setWrapperHeight: function(stickyElement) {
  173. var element = $(stickyElement);
  174. var stickyWrapper = element.parent();
  175. if (stickyWrapper) {
  176. stickyWrapper.css('height', element.outerHeight());
  177. }
  178. },
  179. setupChangeListeners: function(stickyElement) {
  180. if (window.MutationObserver) {
  181. var mutationObserver = new window.MutationObserver(function(mutations) {
  182. if (mutations[0].addedNodes.length || mutations[0].removedNodes.length) {
  183. methods.setWrapperHeight(stickyElement);
  184. }
  185. });
  186. mutationObserver.observe(stickyElement, {subtree: true, childList: true});
  187. } else {
  188. if (window.addEventListener) {
  189. stickyElement.addEventListener('DOMNodeInserted', function() {
  190. methods.setWrapperHeight(stickyElement);
  191. }, false);
  192. stickyElement.addEventListener('DOMNodeRemoved', function() {
  193. methods.setWrapperHeight(stickyElement);
  194. }, false);
  195. } else if (window.attachEvent) {
  196. stickyElement.attachEvent('onDOMNodeInserted', function() {
  197. methods.setWrapperHeight(stickyElement);
  198. });
  199. stickyElement.attachEvent('onDOMNodeRemoved', function() {
  200. methods.setWrapperHeight(stickyElement);
  201. });
  202. }
  203. }
  204. },
  205. update: scroller,
  206. unstick: function(options) {
  207. return this.each(function() {
  208. var that = this;
  209. var unstickyElement = $(that);
  210. var removeIdx = -1;
  211. var i = sticked.length;
  212. while (i-- > 0) {
  213. if (sticked[i].stickyElement.get(0) === that) {
  214. splice.call(sticked,i,1);
  215. removeIdx = i;
  216. }
  217. }
  218. if(removeIdx !== -1) {
  219. unstickyElement.unwrap();
  220. unstickyElement
  221. .css({
  222. 'width': '',
  223. 'position': '',
  224. 'top': '',
  225. 'float': '',
  226. 'z-index': ''
  227. })
  228. ;
  229. }
  230. });
  231. }
  232. };
  233. // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
  234. if (window.addEventListener) {
  235. window.addEventListener('scroll', scroller, false);
  236. window.addEventListener('resize', resizer, false);
  237. } else if (window.attachEvent) {
  238. window.attachEvent('onscroll', scroller);
  239. window.attachEvent('onresize', resizer);
  240. }
  241. $.fn.sticky = function(method) {
  242. if (methods[method]) {
  243. return methods[method].apply(this, slice.call(arguments, 1));
  244. } else if (typeof method === 'object' || !method ) {
  245. return methods.init.apply( this, arguments );
  246. } else {
  247. $.error('Method ' + method + ' does not exist on jQuery.sticky');
  248. }
  249. };
  250. $.fn.unstick = function(method) {
  251. if (methods[method]) {
  252. return methods[method].apply(this, slice.call(arguments, 1));
  253. } else if (typeof method === 'object' || !method ) {
  254. return methods.unstick.apply( this, arguments );
  255. } else {
  256. $.error('Method ' + method + ' does not exist on jQuery.sticky');
  257. }
  258. };
  259. $(function() {
  260. setTimeout(scroller, 0);
  261. });
  262. }));