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