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 } else { 139 o.stickySidebarPaddingTop = 1; 140 } 141 142 if (collapsedBottomHeight == 0) { 143 o.stickySidebar.css('padding-bottom', 0); 144 o.stickySidebarPaddingBottom = 0; 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.sidebar.offset().top + (o.paddingTop - o.options.additionalMarginTop)) { 185 // The top and bottom offsets, used in various calculations. 186 var offsetTop = o.paddingTop + 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.sidebar.offset().top; 191 var containerBottom = o.sidebar.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 } 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 } 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 } else if (!sidebarSameHeightAsContainer && top == windowOffsetBottom - o.stickySidebar.outerHeight()) { 241 position = 'fixed'; 242 } else if (scrollTop + top - o.sidebar.offset().top - o.paddingTop <= options.additionalMarginTop) { 243 // Stuck to the top of the page. No special behavior. 244 position = 'static'; 245 } else { 246 // Stuck to the bottom of the page. 247 position = 'absolute'; 248 } 249 } 250 251 /*
252 * Performance notice: It's OK to set these CSS values at each resize/scroll, even if they don't change. 253 * It's way slower to first check if the values have changed. 254 */ 255 if (position == 'fixed') { 256 var scrollLeft = $(document).scrollLeft(); 257 258 o.stickySidebar.css({ 259 'position': 'fixed', 260 'width': getWidthForObject(o.stickySidebar) + 'px', 261 'transform': 'translateY(' + top + 'px)', 262 'left': (o.sidebar.offset().left + parseInt(o.sidebar.css('padding-left')) - scrollLeft) + 'px', 263 'top': '0px' 264 }); 265 } else if (position == 'absolute') { 266 var css = {}; 267 268 if (o.stickySidebar.css('position') != 'absolute') { 269 css.position = 'absolute'; 270 css.transform = 'translateY(' + (scrollTop + top - o.sidebar.offset().top - o.stickySidebarPaddingTop - o.stickySidebarPaddingBottom) + 'px)'; 271 css.top = '0px'; 272 } 273 274 css.width = getWidthForObject(o.stickySidebar) + 'px'; 275 css.left = ''; 276 277 o.stickySidebar.css(css); 278 } else if (position == 'static') { 279 resetSidebar(); 280 } 281 282 if (position != 'static') { 283 if (o.options.updateSidebarHeight == true) { 284 o.sidebar.css({ 285 'min-height': o.stickySidebar.outerHeight() + o.stickySidebar.offset().top - o.sidebar.offset().top + o.paddingBottom 286 }); 287 } 288 } 289 290 o.previousScrollTop = scrollTop; 291 }; 292 293 // Initialize the sidebar's position. 294 o.onScroll(o); 295 296 // Recalculate the sidebar's position on every scroll and resize. 297 $(document).on('scroll.' + o.options.namespace, function (o) { 298 return function () { 299 o.onScroll(o); 300 }; 301 }(o)); 302 $(window).on('resize.' + o.options.namespace, function (o) { 303 return function () { 304 o.stickySidebar.css({'position': 'static'}); 305 o.onScroll(o); 306 }; 307 }(o)); 308 309 // Recalculate the sidebar's position every time the sidebar changes its size. 310 if (typeof ResizeSensor !== 'undefined') { 311 new ResizeSensor(o.stickySidebar[0], function (o) { 312 return function () { 313 o.onScroll(o); 314 }; 315 }(o)); 316 } 317 318 // Reset the sidebar to its default state 319 function resetSidebar() { 320 o.fixedScrollTop = 0; 321 o.sidebar.css({ 322 'min-height': '1px' 323 }); 324 o.stickySidebar.css({ 325 'position': 'static', 326 'width': '', 327 'transform': 'none' 328 }); 329 } 330 331 // 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. 332 function getClearedHeight(e) { 333 var height = e.height(); 334 335 e.children().each(function () { 336 height = Math.max(height, $(this).height()); 337 }); 338 339 return height; 340 } 341 }); 342 } 343 344 function getWidthForObject(object) { 345 var width; 346 347 try { 348 width = object[0].getBoundingClientRect().width; 349 } catch (err) { 350 } 351 352 if (typeof width === "undefined") { 353 width = object.width(); 354 } 355 356 return width; 357 } 358 359 return this; 360 } 361})(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.