1/* 2 * Sticky Plugin v1.0.0 for jQuery 3 * Free to use and abuse under the MIT license. 4 */ 5 6(function($) { 7 var defaults = { 8 topSpacing: 0, 9 bottomSpacing: 650, 10 className: 'is-sticky', 11 wrapperClassName: 'sticky-wrapper', 12 center: false, 13 getWidthFrom: '' 14 }, 15 $window = $(window), 16 $document = $(document), 17 sticked = [], 18 windowHeight = $window.height(), 19 scroller = function() { 20 var scrollTop = $window.scrollTop(), 21 documentHeight = $document.height(), 22 dwh = documentHeight - windowHeight, 23 extra = (scrollTop > dwh) ? dwh - scrollTop : 0; 24 25 for (var i = 0; i < sticked.length; i++) { 26 var s = sticked[i], 27 elementTop = s.stickyWrapper.offset().top, 28 etse = elementTop - s.topSpacing - extra; 29 30 if (scrollTop <= etse) { 31 if (s.currentTop !== null) { 32 s.stickyElement 33 .css('position', '') 34 .css('top', ''); 35 s.stickyElement.parent().removeClass(s.className); 36 s.currentTop = null; 37 } 38 } 39 else { 40 var newTop = documentHeight - s.stickyElement.outerHeight() 41 - s.topSpacing - s.bottomSpacing - scrollTop - extra; 42 if (newTop < 0) { 43 newTop = newTop + s.topSpacing; 44 } else { 45 newTop = s.topSpacing; 46 } 47 if (s.currentTop != newTop) { 48 s.stickyElement 49 .css('position', 'fixed') 50 .css('top', newTop); 51 52 if (typeof s.getWidthFrom !== 'undefined') { 53 s.stickyElement.css('width', $(s.getWidthFrom).width()); 54 } 55 56 s.stickyElement.parent().addClass(s.className); 57 s.currentTop = newTop; 58 } 59 } 60 } 61 }, 62 resizer = function() { 63 windowHeight = $window.height(); 64 }, 65 methods = { 66 init: function(options) { 67 var o = $.extend(defaults, options); 68 return this.each(function() { 69 var stickyElement = $(this); 70 71 var stickyId = stickyElement.attr('id'); 72 var wrapper = $('<div></div>') 73 .attr('id', stickyId + '-sticky-wrapper') 74 .addClass(o.wrapperClassName); 75 stickyElement.wrapAll(wrapper); 76 77 if (o.center) { 78 stickyElement.parent().css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"}); 79 } 80 81 if (stickyElement.css("float") == "right") { 82 stickyElement.css({"float":"none"}).parent().css({"float":"right"}); 83 } 84 85 var stickyWrapper = stickyElement.parent(); 86 stickyWrapper.css('height', stickyElement.outerHeight()); 87 sticked.push({ 88 topSpacing: o.topSpacing, 89 bottomSpacing: o.bottomSpacing, 90 stickyElement: stickyElement, 91 currentTop: null, 92 stickyWrapper: stickyWrapper, 93 className: o.className, 94 getWidthFrom: o.getWidthFrom 95 }); 96 }); 97 }, 98 update: scroller 99 }; 100 101 // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): 102 if (window.addEventListener) { 103 window.addEventListener('scroll', scroller, false); 104 window.addEventListener('resize', resizer, false); 105 } else if (window.attachEvent) { 106 window.attachEvent('onscroll', scroller); 107 window.attachEvent('onresize', resizer); 108 } 109 110 $.fn.sticky = function(method) { 111 if (methods[method]) { 112 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 113 } else if (typeof method === 'object' || !method ) { 114 return methods.init.apply( this, arguments ); 115 } else { 116 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 117 } 118 }; 119 $(function() { 120 setTimeout(scroller, 0); 121 }); 122})(jQuery); 123
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.