PageSourceSearch

https://prnews.pl/wp-content/themes/htmag/js/theia-sticky-sidebar.js

js prnews.pl collected 2026-09-24 18:30:34 UTC 14,921 bytes, 335 lines download raw bytes

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