1/*! 2 * Theia Sticky Sidebar v1.5.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('TSS: 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 'margin-bottom': '20px', 100 101 // The "box-sizing" must be set to "content-box" because we set a fixed height to this element when the sticky
101sidebar has a fixed position. 102 '-webkit-box-sizing': 'border-box', 103 '-moz-box-sizing': 'border-box', 104 'box-sizing': 'border-box' 105 }); 106 107 // Get the sticky sidebar element. If none has been found, then create one. 108 o.stickySidebar = o.sidebar.find('.theiaStickySidebar'); 109 if (o.stickySidebar.length == 0) { 110 // Remove <script> tags, otherwise they will be run again when added to the stickySidebar. 111 var javaScriptMIMETypes = /(?:text|application)\/(?:x-)?(?:javascript|ecmascript)/i; 112 o.sidebar.find('script').filter(function(index, script) { 113 return script.type.length === 0 || script.type.match(javaScriptMIMETypes); 114 }).remove(); 115 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.marginBottom = parseInt(o.sidebar.css('margin-bottom')); 122 o.paddingTop = parseInt(o.sidebar.css('padding-top')); 123 o.paddingBottom = parseInt(o.sidebar.css('padding-bottom')); 124
125 // Add a temporary padding rule to check for collapsable margins. 126 var collapsedTopHeight = o.stickySidebar.offset().top; 127 var collapsedBottomHeight = o.stickySidebar.outerHeight(); 128 o.stickySidebar.css('padding-top', 1); 129 o.stickySidebar.css('padding-bottom', 1); 130 collapsedTopHeight -= o.stickySidebar.offset().top; 131 collapsedBottomHeight = o.stickySidebar.outerHeight() - collapsedBottomHeight - collapsedTopHeight; 132 if (collapsedTopHeight == 0) { 133 o.stickySidebar.css('padding-top', 0); 134 o.stickySidebarPaddingTop = 0; 135 } 136 else { 137 o.stickySidebarPaddingTop = 1; 138 } 139 140 if (collapsedBottomHeight == 0) { 141 o.stickySidebar.css('padding-bottom', 0); 142 o.stickySidebarPaddingBottom = 0; 143 } 144 else { 145 o.stickySidebarPaddingBottom = 1; 146 } 147 148 // We use this to know whether the user is scrolling up or down. 149 o.previousScrollTop = null; 150 151 // Scroll top (value) when the sidebar has fixed position. 152 o.fixedScrollTop = 0; 153 154 // Set sidebar to default values. 155 resetSidebar(); 156 157 o.onScroll = function (o) { 158 // Stop if the sidebar isn't visible. 159 if (!o.stickySidebar.is(":visible")) { 160 return; 161 } 162 163 // Stop if the window is too small. 164 if ($('body').width() < o.options.minWidth) { 165 resetSidebar(); 166 return; 167 } 168 169 // 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) 170 if (o.options.disableOnResponsiveLayouts) { 171 var sidebarWidth = o.sidebar.outerWidth(o.sidebar.css('float') == 'none'); 172 173 if (sidebarWidth + 50 > o.container.width()) { 174 resetSidebar(); 175 return; 176 } 177 } 178 179 var scrollTop = $(document).scrollTop(); 180 var position = 'static'; 181 182 // If the user has scrolled down enough for the sidebar to be clipped at the top, then we can consider changing its position. 183 if (scrollTop >= o.sidebar.offset().top + (o.paddingTop - o.options.additionalMarginTop)) { 184 // The top and bottom offsets, used in various calculations. 185 var offsetTop = o.paddingTop + options.additionalMarginTop; 186 var offsetBottom = o.paddingBottom + o.marginBottom + options.additionalMarginBottom; 187 188 // All top and bottom positions are relative to the window, not to the parent elemnts. 189 var containerTop = o.sidebar.offset().top; 190 var containerBottom = o.sidebar.offset().top + getClearedHeight(o.container); 191 192 // The top and bottom offsets relative to the window screen top (zero) and bottom (window height). 193 var windowOffsetTop = 0 + options.additionalMarginTop; 194 var windowOffsetBottom; 195 196 var sidebarSmallerThanWindow = (o.stickySidebar.outerHeight() + offsetTop + offsetBottom) < $(window).height(); 197 if (sidebarSmallerThanWindow) { 198 windowOffsetBottom = windowOffsetTop + o.stickySidebar.outerHeight(); 199 } 200 else { 201 windowOffsetBottom = $(window).height() - o.marginBottom - o.paddingBottom - options.additionalMarginBottom; 202 } 203 204 var staticLimitTop = containerTop - scrollTop + o.paddingTop; 205 var staticLimitBottom = containerBottom - scrollTop - o.paddingBottom - o.marginBottom; 206 207 var top = o.stickySidebar.offset().top - scrollTop; 208 var scrollTopDiff = o.previousScrollTop - scrollTop; 209 210 // If the sidebar position is fixed, then it won't move up or down by itself. So, we manually adjust the top coordinate. 211 if (o.stickySidebar.css('position') == 'fixed') { 212 if (o.options.sidebarBehavior == 'modern') { 213 top += scrollTopDiff; 214 } 215 } 216 217 if (o.options.sidebarBehavior == 'stick-to-top') { 218 top = options.additionalMarginTop; 219 } 220 221 if (o.options.sidebarBehavior == 'stick-to-bottom') { 222 top = windowOffsetBottom - o.stickySidebar.outerHeight(); 223 } 224 225 if (scrollTopDiff > 0) { // If the user is scrolling up. 226 top = Math.min(top, windowOffsetTop); 227 } 228 else { // If the user is scrolling down. 229 top = Math.max(top, windowOffsetBottom - o.stickySidebar.outerHeight()); 230 } 231 232 top = Math.max(top, staticLimitTop); 233 234 top = Math.min(top, staticLimitBottom - o.stickySidebar.outerHeight()); 235 236 // If the sidebar is the same height as the container, we won't use fixed positioning. 237 var sidebarSameHeightAsContainer = o.container.height() == o.stickySidebar.outerHeight(); 238 239 if (!sidebarSameHeightAsContainer && top == windowOffsetTop) { 240 position = 'fixed'; 241 } 242 else if (!sidebarSameHeightAsContainer && top == windowOffsetBottom - o.stickySidebar.outerHeight()) { 243 position = 'fixed'; 244 } 245 else if (scrollTop + top - o.sidebar.offset().top - o.paddingTop <= options.additionalMarginTop) { 246 // Stuck to the top of the page. No special behavior. 247 position = 'static'; 248 } 249 else { 250 // Stuck to the bottom of the page. 251 position = 'absolute'; 252 } 253 } 254 255 /*
256 * Performance notice: It's OK to set these CSS values at each resize/scroll, even if they don't change. 257 * It's way slower to first check if the values have changed. 258 */ 259 if (position == 'fixed') { 260 o.stickySidebar.css({ 261 'position': 'fixed', 262 'width': getWidthForObject(o.stickySidebar) + 'px', 263 'transform': 'translateY(' + top + 'px)', 264 'left': (o.sidebar.offset().left + parseInt(o.sidebar.css('padding-left'))) + 'px', 265 'top': '0px' 266 }); 267 } 268 else if (position == 'absolute') { 269 var css = {}; 270 271 if (o.stickySidebar.css('position') != 'absolute') { 272 css.position = 'absolute'; 273 css.transform = 'translateY(' + (scrollTop + top - o.sidebar.offset().top - o.stickySidebarPaddingTop - o.stickySidebarPaddingBottom) + 'px)'; 274 css.top = '0px'; 275 } 276 277 css.width = getWidthForObject(o.stickySidebar) + 'px'; 278 css.left = ''; 279 280 o.stickySidebar.css(css); 281 } 282 else if (position == 'static') { 283 resetSidebar(); 284 } 285 286 if (position != 'static') { 287 if (o.options.updateSidebarHeight == true) { 288 o.sidebar.css({ 289 'min-height': o.stickySidebar.outerHeight() + o.stickySidebar.offset().top - o.sidebar.offset().top + o.paddingBottom 290 }); 291 } 292 } 293 294 o.previousScrollTop = scrollTop; 295 }; 296 297 // Initialize the sidebar's position. 298 o.onScroll(o); 299 300 // Recalculate the sidebar's position on every scroll and resize. 301 $(document).scroll(function (o) { 302 return function () { 303 o.onScroll(o); 304 }; 305 }(o)); 306 $(window).resize(function (o) { 307 return function () { 308 o.stickySidebar.css({'position': 'static'}); 309 o.onScroll(o); 310 }; 311 }(o)); 312 313 // Reset the sidebar to its default state 314 function resetSidebar() { 315 o.fixedScrollTop = 0; 316 o.sidebar.css({ 317 'min-height': '1px' 318 }); 319 o.stickySidebar.css({ 320 'position': 'static', 321 'width': '', 322 'transform': 'none' 323 }); 324 } 325 326 // 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. 327 function getClearedHeight(e) { 328 var height = e.height(); 329 330 e.children().each(function () { 331 height = Math.max(height, $(this).height()); 332 }); 333 334 return height; 335 } 336 }); 337 } 338 339 function getWidthForObject(object) { 340 var width; 341 342 try { 343 width = object[0].getBoundingClientRect().width; 344 } 345 catch(err) { 346 } 347 348 if (typeof width === "undefined") { 349 width = object.width(); 350 } 351 352 return width; 353 } 354 } 355})(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.