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