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