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