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('position', '') 42 .css('top', ''); 43 s.stickyElement.trigger('sticky-end', [s]).parent().removeClass(s.className); 44 s.currentTop = null; 45 } 46 } 47 else { 48 var newTop = documentHeight - s.stickyElement.outerHeight() 49 - s.topSpacing - s.bottomSpacing - scrollTop - extra; 50 if (newTop < 0) { 51 newTop = newTop + s.topSpacing; 52 } else { 53 newTop = s.topSpacing; 54 } 55 if (s.currentTop != newTop) { 56 s.stickyElement 57 .css('position', 'fixed') 58 .css('top', newTop); 59 60 if (typeof s.getWidthFrom !== 'undefined') { 61 s.stickyElement.css('width', $(s.getWidthFrom).width()); 62 } 63 64 s.stickyElement.trigger('sticky-start', [s]).parent().addClass(s.className); 65 s.currentTop = newTop; 66 } 67 } 68 } 69 }, 70 resizer = function() { 71 windowHeight = $window.height(); 72 73 for (var i = 0; i < sticked.length; i++) { 74 var s = sticked[i]; 75 if (typeof s.getWidthFrom !== 'undefined' && s.responsiveWidth === true) { 76 s.stickyElement.css('width', $(s.getWidthFrom).width()); 77 } 78 } 79 }, 80 methods = { 81 init: function(options) { 82 var o = $.extend({}, defaults, options); 83 return this.each(function() { 84 var stickyElement = $(this); 85 86 var stickyId = stickyElement.attr('id'); 87 var wrapperId = stickyId ? stickyId + '-' + defaults.wrapperClassName : defaults.wrapperClassName 88 var wrapper = $('<div></div>') 89 .attr('id', stickyId + '-sticky-wrapper') 90 .addClass(o.wrapperClassName); 91 stickyElement.wrapAll(wrapper); 92 93 if (o.center) { 94 stickyElement.parent().css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"}); 95 } 96 97 if (stickyElement.css("float") == "right") { 98 stickyElement.css({"float":"none"}).parent().css({"float":"right"}); 99 } 100 101 var stickyWrapper = stickyElement.parent(); 102 stickyWrapper.css('height', stickyElement.outerHeight()); 103 sticked.push({ 104 topSpacing: o.topSpacing, 105 bottomSpacing: o.bottomSpacing, 106 stickyElement: stickyElement, 107 currentTop: null, 108 stickyWrapper: stickyWrapper, 109 className: o.className, 110 getWidthFrom: o.getWidthFrom, 111 responsiveWidth: o.responsiveWidth 112 }); 113 }); 114 }, 115 update: scroller, 116 unstick: function(options) { 117 return this.each(function() { 118 var unstickyElement = $(this); 119 120 var removeIdx = -1; 121 for (var i = 0; i < sticked.length; i++) 122 { 123 if (sticked[i].stickyElement.get(0) == unstickyElement.get(0)) 124 { 125 removeIdx = i; 126 } 127 } 128 if(removeIdx != -1) 129 { 130 sticked.splice(removeIdx,1); 131 unstickyElement.unwrap(); 132 unstickyElement.removeAttr('style'); 133 } 134 }); 135 } 136 }; 137 138 // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): 139 if (window.addEventListener) { 140 window.addEventListener('scroll', scroller, false); 141 window.addEventListener('resize', resizer, false); 142 } else if (window.attachEvent) { 143 window.attachEvent('onscroll', scroller); 144 window.attachEvent('onresize', resizer); 145 } 146 147 $.fn.sticky = function(method) { 148 if (methods[method]) { 149 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 150 } else if (typeof method === 'object' || !method ) { 151 return methods.init.apply( this, arguments ); 152 } else { 153 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 154 } 155 }; 156 157 $.fn.unstick = function(method) { 158 if (methods[method]) { 159 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 160 } else if (typeof method === 'object' || !method ) { 161 return methods.unstick.apply( this, arguments ); 162 } else { 163 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 164 } 165 166 }; 167 $(function() { 168 setTimeout(scroller, 0); 169 }); 170})(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.