PageSourceSearch

https://uniza.sk/templates/shaper_helix3/js/jquery.sticky.js

js uniza.sk collected 2026-09-24 08:58:10 UTC 8,979 bytes, 301 lines download raw bytes

vendor: 8,979 bytes, lines 1-301
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: "inherit",
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.css({
60							width: "",
61							position: "",
62							top: "",
63							"z-index": "",
64						});
65						s.stickyElement.parent().removeClass(s.className);
66						s.stickyElement.trigger("sticky-end", [s]);
67						s.currentTop = null;
68					}
69				} else {
70					var newTop =
71						documentHeight -
72						s.stickyElement.outerHeight() -
73						s.topSpacing -
74						s.bottomSpacing -
75						scrollTop -
76						extra;
77					if (newTop < 0) {
78						newTop = newTop + s.topSpacing;
79					} else {
80						newTop = s.topSpacing;
81					}
82					if (s.currentTop !== newTop) {
83						var newWidth;
84						if (s.getWidthFrom) {
85							padding = s.stickyElement.innerWidth() - s.stickyElement.width();
86							newWidth = $(s.getWidthFrom).width() - padding || null;
87						} else if (s.widthFromWrapper) {
88							newWidth = s.stickyWrapper.width();
89						}
90						if (newWidth == null) {
91							newWidth = s.stickyElement.width();
92						}
93						s.stickyElement
94							.css("width", newWidth)
95							.css("position", "fixed")
96							.css("top", newTop)
97							.css("z-index", s.zIndex);
98
99						s.stickyElement.parent().addClass(s.className);
100
101						if (s.currentTop === null) {
102							s.stickyElement.trigger("sticky-start", [s]);
103						} else {
104							// sticky is started but it have to be repositioned
105							s.stickyElement.trigger("sticky-update", [s]);
106						}
107
108						if (
109							(s.currentTop === s.topSpacing && s.currentTop > newTop) ||
110							(s.currentTop === null && newTop < s.topSpacing)
111						) {
112							// just reached bottom || just started to stick but bottom is already reached
113							s.stickyElement.trigger("sticky-bottom-reached", [s]);
114						} else if (s.currentTop !== null && newTop === s.topSpacing && s.currentTop < newTop) {
115							// sticky is started && sticked at topSpacing && overflowing from top just finished
116							s.stickyElement.trigger("sticky-bottom-unreached", [s]);
117						}
118
119						s.currentTop = newTop;
120					}
121
122					// Check if sticky has reached end of container and stop sticking
123					var stickyWrapperContainer = s.stickyWrapper.parent();
124					var unstick =
125						s.stickyElement.offset().top + s.stickyElement.outerHeight() >=
126							stickyWrapperContainer.offset().top + stickyWrapperContainer.outerHeight() &&
127						s.stickyElement.offset().top <= s.topSpacing;
128
129					if (unstick) {
130						s.stickyElement.css("position", "absolute").css("top", "").css("bottom", 0).css("z-index", "");
131					} else {
132						s.stickyElement
133							.css("position", "fixed")
134							.css("top", newTop)
135							.css("bottom", "")
136							.css("z-index", s.zIndex);
137					}
138				}
139			}
140		},
141		resizer = function () {
142			windowHeight = $window.height();
143
144			for (var i = 0, l = sticked.length; i < l; i++) {
145				var s = sticked[i];
146				var newWidth = null;
147				if (s.getWidthFrom) {
148					if (s.responsiveWidth) {
149						newWidth = $(s.getWidthFrom).width();
150					}
151				} else if (s.widthFromWrapper) {
152					newWidth = s.stickyWrapper.width();
153				}
154				if (newWidth != null) {
155					s.stickyElement.css("width", newWidth);
156				}
157			}
158		},
159		methods = {
160			init: function (options) {
161				return this.each(function () {
162					var o = $.extend({}, defaults, options);
163					var stickyElement = $(this);
164
165					var stickyId = stickyElement.attr("id");
166					var wrapperId = stickyId ? stickyId + "-" + defaults.wrapperClassName : defaults.wrapperClassName;
167					var wrapper = $("<div></div>").attr("id", wrapperId).addClass(o.wrapperClassName);
168
169					stickyElement.wrapAll(function () {
170						if ($(this).parent("#" + wrapperId).length == 0) {
171							return wrapper;
172						}
173					});
174
175					var stickyWrapper = stickyElement.parent();
176
177					if (o.center) {
178						stickyWrapper.css({
179							width: stickyElement.outerWidth(),
180							marginLeft: "auto",
181							marginRight: "auto",
182						});
183					}
184
185					if (stickyElement.css("float") === "right") {
186						stickyElement.css({ float: "none" }).parent().css({ float: "right" });
187					}
188
189					o.stickyElement = stickyElement;
190					o.stickyWrapper = stickyWrapper;
191					o.currentTop = null;
192
193					sticked.push(o);
194
195					methods.setWrapperHeight(this);
196					methods.setupChangeListeners(this);
197				});
198			},
199
200			setWrapperHeight: function (stickyElement) {
201				var element = $(stickyElement);
202				var stickyWrapper = element.parent();
203				if (stickyWrapper) {
204					stickyWrapper.css("height", element.outerHeight());
205				}
206			},
207
208			setupChangeListeners: function (stickyElement) {
209				if (window.MutationObserver) {
210					var mutationObserver = new window.MutationObserver(function (mutations) {
211						if (mutations[0].addedNodes.length || mutations[0].removedNodes.length) {
212							methods.setWrapperHeight(stickyElement);
213						}
214					});
215					mutationObserver.observe(stickyElement, { subtree: true, childList: true });
216				} else {
217					if (window.addEventListener) {
218						stickyElement.addEventListener(
219							"DOMNodeInserted",
220							function () {
221								methods.setWrapperHeight(stickyElement);
222							},
223							false
224						);
225						stickyElement.addEventListener(
226							"DOMNodeRemoved",
227							function () {
228								methods.setWrapperHeight(stickyElement);
229							},
230							false
231						);
232					} else if (window.attachEvent) {
233						stickyElement.attachEvent("onDOMNodeInserted", function () {
234							methods.setWrapperHeight(stickyElement);
235						});
236						stickyElement.attachEvent("onDOMNodeRemoved", function () {
237							methods.setWrapperHeight(stickyElement);
238						});
239					}
240				}
241			},
242			update: scroller,
243			unstick: function (options) {
244				return this.each(function () {
245					var that = this;
246					var unstickyElement = $(that);
247
248					var removeIdx = -1;
249					var i = sticked.length;
250					while (i-- > 0) {
251						if (sticked[i].stickyElement.get(0) === that) {
252							splice.call(sticked, i, 1);
253							removeIdx = i;
254						}
255					}
256					if (removeIdx !== -1) {
257						unstickyElement.unwrap();
258						unstickyElement.css({
259							width: "",
260							position: "",
261							top: "",
262							float: "",
263							"z-index": "",
264						});
265					}
266				});
267			},
268		};
269
270	// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
271	if (window.addEventListener) {
272		window.addEventListener("scroll", scroller, false);
273		window.addEventListener("resize", resizer, false);
274	} else if (window.attachEvent) {
275		window.attachEvent("onscroll", scroller);
276		window.attachEvent("onresize", resizer);
277	}
278
279	$.fn.sticky = function (method) {
280		if (methods[method]) {
281			return methods[method].apply(this, slice.call(arguments, 1));
282		} else if (typeof method === "object" || !method) {
283			return methods.init.apply(this, arguments);
284		} else {
285			$.error("Method " + method + " does not exist on jQuery.sticky");
286		}
287	};
288
289	$.fn.unstick = function (method) {
290		if (methods[method]) {
291			return methods[method].apply(this, slice.call(arguments, 1));
292		} else if (typeof method === "object" || !method) {
293			return methods.unstick.apply(this, arguments);
294		} else {
295			$.error("Method " + method + " does not exist on jQuery.sticky");
296		}
297	};
298	$(function () {
299		setTimeout(scroller, 0);
300	});
301});

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.