PageSourceSearch

https://mibeacon.net/wp-content/themes/aeonblog/js/theia-sticky-sidebar.js?ver=20151215

js mibeacon.net collected 2026-10-10 05:16:56 UTC 11,016 bytes, 337 lines download raw bytes

1/*!
2 * File aeonblog.
3 * @package   AeonBlog
4 * @author    AeonWP <[email protected]>
5 * @copyright Copyright (c) 2019, AeonWP
6 * @link      https://aeonwp.com/aeonblog
7 * @license   http://www.gnu.org/licenses/gpl-2.0.html
8 * 
9 * Theia Sticky Sidebar v1.4.0
10 * https://github.com/WeCodePixels/theia-sticky-sidebar
11 *
12 * Glues your website's sidebars, making them permanently visible while scrolling.
13 *
14 * Copyright 2013-2016 WeCodePixels and other contributors
15 * Released under the MIT license
16 */
17
18(function ($) {
19	$.fn.theiaStickySidebar = function (options) {
20		var defaults = {
21			'containerSelector': '',
22			'additionalMarginTop': 0,
23			'additionalMarginBottom': 0,
24			'updateSidebarHeight': true,
25			'minWidth': 0,
26			'disableOnResponsiveLayouts': true,
27			'sidebarBehavior': 'modern'
28		};
29		options = $.extend(defaults, options);
30
31		// Validate options
32		options.additionalMarginTop = parseInt(options.additionalMarginTop) || 0;
33		options.additionalMarginBottom = parseInt(options.additionalMarginBottom) || 0;
34
35		tryInitOrHookIntoEvents(options, this);
36
37		// Try doing init, otherwise hook into window.resize and document.scroll and try again then.
38		function tryInitOrHookIntoEvents(options, $that) {
39			var success = tryInit(options, $that);
40
41			if (!success) {
42				console.log('TST: Body width smaller than options.minWidth. Init is delayed.');
43
44				$(document).scroll(function (options, $that) {
45					return function (evt) {
46						var success = tryInit(options, $that);
47
48						if (success) {
49							$(this).unbind(evt);
50						}
51					};
52				}(options, $that));
53				$(window).resize(function (options, $that) {
54					return function (evt) {
55						var success = tryInit(options, $that);
56
57						if (success) {
58							$(this).unbind(evt);
59						}
60					};
61				}(options, $that))
62			}
63		}
64
65		// Try doing init if proper conditions are met.
66		function tryInit(options, $that) {
67			if (options.initialized === true) {
68				return true;
69			}
70
71			if ($('body').width() < options.minWidth) {
72				return false;
73			}
74
75			init(options, $that);
76
77			return true;
78		}
79
80		// Init the sticky sidebar(s).
81		function init(options, $that) {
82			options.initialized = true;
83
84			// Add CSS
85			$('head').append($('<style>.theiaStickySidebar:after {content: ""; display: table; clear: both;}</style>'));
86
87			$that.each(function () {
88				var o = {};
89
90				o.sidebar = $(this);
91
92				// Save options
93				o.options = options || {};
94
95				// Get container
96				o.container = $(o.options.containerSelector);
97				if (o.container.size() == 0) {
98					o.container = o.sidebar.parent();
99				}
100
101				// Create sticky sidebar
102				o.sidebar.parents().css('-webkit-transform', 'none'); // Fix for WebKit bug - https://code.google.com/p/chromium/issues/detail?id=20574
103				o.sidebar.css({
104					'position': 'relative',
105					'overflow': 'visible',
106					// The "box-sizing" must be set to "content-box" because we set a fixed height to this element when the sticky 
106sidebar has a fixed position.
107					'-webkit-box-sizing': 'border-box',
108					'-moz-box-sizing': 'border-box',
109					'box-sizing': 'border-box'
110				});
111
112				// Get the sticky sidebar element. If none has been found, then create one.
113				o.stickySidebar = o.sidebar.find('.theiaStickySidebar');
114				if (o.stickySidebar.length == 0) {
115					o.sidebar.find('script').remove(); // Remove <script> tags, otherwise they will be run again on the next line.
116					o.stickySidebar = $('<div>').addClass('theiaStickySidebar').append(o.sidebar.children());
117					o.sidebar.append(o.stickySidebar);
118				}
119
120				// Get existing top and bottom margins and paddings
121				o.marginTop = parseInt(o.sidebar.css('margin-top'));
122				o.marginBottom = parseInt(o.sidebar.css('margin-bottom'));
123				o.paddingTop = parseInt(o.sidebar.css('padding-top'));
124				o.paddingBottom = parseInt(o.sidebar.css('padding-bottom'));
125
126				// Add a temporary padding rule to check for collapsable margins.
127				var collapsedTopHeight = o.stickySidebar.offset().top;
128				var collapsedBottomHeight = o.stickySidebar.outerHeight();
129				o.stickySidebar.css('padding-top', 1);
130				o.stickySidebar.css('padding-bottom', 1);
131				collapsedTopHeight -= o.stickySidebar.offset().top;
132				collapsedBottomHeight = o.stickySidebar.outerHeight() - collapsedBottomHeight - collapsedTopHeight;
133				if (collapsedTopHeight == 0) {
134					o.stickySidebar.css('padding-top', 0);
135					o.stickySidebarPaddingTop = 0;
136				}
137				else {
138					o.stickySidebarPaddingTop = 1;
139				}
140
141				if (collapsedBottomHeight == 0) {
142					o.stickySidebar.css('padding-bottom', 0);
143					o.stickySidebarPaddingBottom = 0;
144				}
145				else {
146					o.stickySidebarPaddingBottom = 1;
147				}
148
149				// We use this to know whether the user is scrolling up or down.
150				o.previousScrollTop = null;
151
152				// Scroll top (value) when the sidebar has fixed position.
153				o.fixedScrollTop = 0;
154
155				// Set sidebar to default values.
156				resetSidebar();
157
158				o.onScroll = function (o) {
159					// Stop if the sidebar isn't visible.
160					if (!o.stickySidebar.is(":visible")) {
161						return;
162					}
163
164					// Stop if the window is too small.
165					if ($('body').width() < o.options.minWidth) {
166						resetSidebar();
167						return;
168					}
169
170					// Stop if the sidebar width is larger than the container width (e.g. the theme is responsive and the sidebar is now below the content)
171					if (o.options.disableOnResponsiveLayouts) {
172						var sidebarWidth = o.sidebar.outerWidth(o.sidebar.css('float') == 'none');
173
174						if (sidebarWidth + 50 > o.container.width()) {
175							resetSidebar();
176							return;
177						}
178					}
179
180					var scrollTop = $(document).scrollTop();
181					var position = 'static';
182
183					// If the user has scrolled down enough for the sidebar to be clipped at the top, then we can consider changing its position.
184					if (scrollTop >= o.container.offset().top + (o.paddingTop + o.marginTop - o.options.additionalMarginTop)) {
185						// The top and bottom offsets, used in various calculations.
186						var offsetTop = o.paddingTop + o.marginTop + options.additionalMarginTop;
187						var offsetBottom = o.paddingBottom + o.marginBottom + options.additionalMarginBottom;
188
189						// All top and bottom positions are relative to the window, not to the parent elemnts.
190						var containerTop = o.container.offset().top;
191						var containerBottom = o.container.offset().top + getClearedHeight(o.container);
192
193						// The top and bottom offsets relative to the window screen top (zero) and bottom (window height).
194						var windowOffsetTop = 0 + options.additionalMarginTop;
195						var windowOffsetBottom;
196
197						var sidebarSmallerThanWindow = (o.stickySidebar.outerHeight() + offsetTop + offsetBottom) < $(window).height();
198						if (sidebarSmallerThanWindow) {
199							windowOffsetBottom = windowOffsetTop + o.stickySidebar.outerHeight();
200						}
201						else {
202							windowOffsetBottom = $(window).height() - o.marginBottom - o.paddingBottom - options.additionalMarginBottom;
203						}
204
205						var staticLimitTop = containerTop - scrollTop + o.paddingTop + o.marginTop;
206						var staticLimitBottom = containerBottom - scrollTop - o.paddingBottom - o.marginBottom;
207
208						var top = o.stickySidebar.offset().top - scrollTop;
209						var scrollTopDiff = o.previousScrollTop - scrollTop;
210
211						// If the sidebar position is fixed, then it won't move up or down by itself. So, we manually adjust the top coordinate.
212						if (o.stickySidebar.css('position') == 'fixed') {
213							if (o.options.sidebarBehavior == 'modern') {
214								top += scrollTopDiff;
215							}
216						}
217
218						if (o.options.sidebarBehavior == 'stick-to-top') {
219							top = options.additionalMarginTop;
220						}
221
222						if (o.options.sidebarBehavior == 'stick-to-bottom') {
223							top = windowOffsetBottom - o.stickySidebar.outerHeight();
224						}
225
226						if (scrollTopDiff > 0) { // If the user is scrolling up.
227							top = Math.min(top, windowOffsetTop);
228						}
229						else { // If the user is scrolling down.
230							top = Math.max(top, windowOffsetBottom - o.stickySidebar.outerHeight());
231						}
232
233						top = Math.max(top, staticLimitTop);
234
235						top = Math.min(top, staticLimitBottom - o.stickySidebar.outerHeight());
236
237						// If the sidebar is the same height as the container, we won't use fixed positioning.
238						var sidebarSameHeightAsContainer = o.container.height() == o.stickySidebar.outerHeight();
239
240						if (!sidebarSameHeightAsContainer && top == windowOffsetTop) {
241							position = 'fixed';
242						}
243						else if (!sidebarSameHeightAsContainer && top == windowOffsetBottom - o.stickySidebar.outerHeight()) {
244							position = 'fixed';
245						}
246						else if (scrollTop + top - o.sidebar.offset().top - o.paddingTop <= options.additionalMarginTop) {
247							// Stuck to the top of the page. No special behavior.
248							position = 'static';
249						}
250						else {
251							// Stuck to the bottom of the page.
252							position = 'absolute';
253						}
254					}
255
256					/*
257					 * Performance notice: It's OK to set these CSS values at each resize/scroll, even if they don't change.
258					 * It's way slower to first check if the values have changed.
259					 */
260					if (position == 'fixed') {
261						o.stickySidebar.css({
262							'position': 'fixed',
263							'width': o.sidebar.width(),
264							'top': top,
265							'left': o.sidebar.offset().left + parseInt(o.sidebar.css('padding-left'))
266						});
267					}
268					else if (position == 'absolute') {
269						var css = {};
270
271						if (o.stickySidebar.css('position') != 'absolute') {
272							css.position = 'absolute';
273							css.top = scrollTop + top - o.sidebar.offset().top - o.stickySidebarPaddingTop - o.stickySidebarPaddingBottom;
274						}
275
276						css.width = o.sidebar.width();
277						css.left = '';
278
279						o.stickySidebar.css(css);
280					}
281					else if (position == 'static') {
282						resetSidebar();
283					}
284
285					if (position != 'static') {
286						if (o.options.updateSidebarHeight == true) {
287							o.sidebar.css({
288								'min-height': o.stickySidebar.outerHeight() + o.stickySidebar.offset().top - o.sidebar.offset().top + o.paddingBottom
289							});
290						}
291					}
292
293					o.previousScrollTop = scrollTop;
294				};
295
296				// Initialize the sidebar's position.
297				o.onScroll(o);
298
299				// Recalculate the sidebar's position on every scroll and resize.
300				$(document).scroll(function (o) {
301					return function () {
302						o.onScroll(o);
303					};
304				}(o));
305				$(window).resize(function (o) {
306					return function () {
307						o.stickySidebar.css({'position': 'static'});
308						o.onScroll(o);
309					};
310				}(o));
311
312				// Reset the sidebar to its default state
313				function resetSidebar() {
314					o.fixedScrollTop = 0;
315					o.sidebar.css({
316						'min-height': '1px'
317					});
318					o.stickySidebar.css({
319						'position': 'static',
320						'width': ''
321					});
322				}
323
324				// Get the height of a div as if its floated children were cleared. Note that this function fails if the floats are more than one level deep.
325				function getClearedHeight(e) {
326					var height = e.height();
327
328					e.children().each(function () {
329						height = Math.max(height, $(this).height());
330					});
331
332					return height;
333				}
334			});
335		}
336	}
337})(jQuery);

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.