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