1// Sticky Plugin v1.0.0 for jQuery 2// ============= 3// Author: Anthony Garand 4// Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk) 5// Improvements by Leonardo C. Daronco (daronco) 6// Created: 2/14/2011 7// Date: 2/12/2012 8// Website: http://labs.anthonygarand.com/sticky 9// Description: Makes an element on the page stick on the screen as you scroll 10// It will only set the 'top' and 'position' of your element, you 11// might need to adjust the width in some cases. 12 13(function($) { 14 var defaults = { 15 topSpacing: 0, 16 bottomSpacing: 0, 17 className: 'is-sticky', 18 wrapperClassName: 'sticky-wrapper', 19 center: false, 20 getWidthFrom: '', 21 responsiveWidth: false 22 }, 23 $window = $(window), 24 $document = $(document), 25 sticked = [], 26 windowHeight = $window.height(), 27 scroller = function() { 28 var scrollTop = $window.scrollTop(), 29 documentHeight = $document.height(), 30 dwh = documentHeight - windowHeight, 31 extra = (scrollTop > dwh) ? dwh - scrollTop : 0; 32 33 for (var i = 0; i < sticked.length; i++) { 34 var s = sticked[i], 35 elementTop = s.stickyWrapper.offset().top, 36 etse = elementTop - s.topSpacing - extra; 37 38 if (scrollTop <= etse) { 39 if (s.currentTop !== null) { 40 s.stickyElement 41 .css('width', '') 42 .css('position', '') 43 .css('top', ''); 44 s.stickyElement.trigger('sticky-end', [s]).parent().removeClass(s.className); 45 s.currentTop = null; 46 } 47 } 48 else { 49 var newTop = documentHeight - s.stickyElement.outerHeight() 50 - s.topSpacing - s.bottomSpacing - scrollTop - extra; 51 if (newTop < 0) { 52 newTop = newTop + s.topSpacing; 53 } else { 54 newTop = s.topSpacing; 55 } 56 if (s.currentTop != newTop) { 57 s.stickyElement 58 .css('width', s.stickyElement.width()) 59 .css('position', 'fixed') 60 .css('top', newTop); 61 62 if (typeof s.getWidthFrom !== 'undefined') { 63 s.stickyElement.css('width', $(s.getWidthFrom).width()); 64 } 65 66 s.stickyElement.trigger('sticky-start', [s]).parent().addClass(s.className); 67 s.currentTop = newTop; 68 } 69 } 70 } 71 }, 72 resizer = function() { 73 windowHeight = $window.height(); 74 75 for (var i = 0; i < sticked.length; i++) { 76 var s = sticked[i]; 77 if (typeof s.getWidthFrom !== 'undefined' && s.responsiveWidth === true) { 78 s.stickyElement.css('width', $(s.getWidthFrom).width()); 79 } 80 } 81 }, 82 methods = { 83 init: function(options) { 84 var o = $.extend({}, defaults, options); 85 return this.each(function() { 86 var stickyElement = $(this); 87 88 var stickyId = stickyElement.attr('id'); 89 var wrapperId = stickyId ? stickyId + '-' + defaults.wrapperClassName : defaults.wrapperClassName 90 var wrapper = $('<div></div>') 91 .attr('id', stickyId + '-sticky-wrapper') 92 .addClass(o.wrapperClassName); 93 stickyElement.wrapAll(wrapper); 94 95 if (o.center) { 96 stickyElement.parent().css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"}); 97 } 98 99 if (stickyElement.css("float") == "right") { 100 stickyElement.css({"float":"none"}).parent().css({"float":"right"}); 101 } 102 103 var stickyWrapper = stickyElement.parent(); 104 stickyWrapper.css('height', stickyElement.outerHeight()); 105 sticked.push({ 106 topSpacing: o.topSpacing, 107 bottomSpacing: o.bottomSpacing, 108 stickyElement: stickyElement, 109 currentTop: null, 110 stickyWrapper: stickyWrapper, 111 className: o.className, 112 getWidthFrom: o.getWidthFrom, 113 responsiveWidth: o.responsiveWidth 114 }); 115 }); 116 }, 117 update: scroller, 118 unstick: function(options) { 119 return this.each(function() { 120 var unstickyElement = $(this); 121 122 var removeIdx = -1; 123 for (var i = 0; i < sticked.length; i++) 124 { 125 if (sticked[i].stickyElement.get(0) == unstickyElement.get(0)) 126 { 127 removeIdx = i; 128 } 129 } 130 if(removeIdx != -1) 131 { 132 sticked.splice(removeIdx,1); 133 unstickyElement.unwrap(); 134 unstickyElement.removeAttr('style'); 135 } 136 }); 137 } 138 }; 139 140 // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): 141 if (window.addEventListener) { 142 window.addEventListener('scroll', scroller, false); 143 window.addEventListener('resize', resizer, false); 144 } else if (window.attachEvent) { 145 window.attachEvent('onscroll', scroller); 146 window.attachEvent('onresize', resizer); 147 } 148 149 $.fn.sticky = function(method) { 150 if (methods[method]) { 151 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 152 } else if (typeof method === 'object' || !method ) { 153 return methods.init.apply( this, arguments ); 154 } else { 155 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 156 } 157 }; 158 159 $.fn.unstick = function(method) { 160 if (methods[method]) { 161 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 162 } else if (typeof method === 'object' || !method ) { 163 return methods.unstick.apply( this, arguments ); 164 } else { 165 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 166 } 167 168 }; 169 $(function() { 170 setTimeout(scroller, 0); 171 }); 172})(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.