PageSourceSearch

https://zippykind.com/assets/jquery.sticky2.js

js zippykind.com collected 2026-10-01 23:53:03 UTC 9,474 bytes, 274 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.