1// Sticky Plugin 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 }, 20 $window = $(window), 21 $document = $(document), 22 sticked = [], 23 windowHeight = $window.height(), 24 scroller = function() { 25 var scrollTop = $window.scrollTop(), 26 documentHeight = $document.height(), 27 dwh = documentHeight - windowHeight, 28 extra = (scrollTop > dwh) ? dwh - scrollTop : 0; 29 for (var i = 0; i < sticked.length; i++) { 30 var s = sticked[i], 31 elementTop = s.stickyWrapper.offset().top, 32 etse = elementTop - s.topSpacing - extra; 33 34 if (scrollTop <= etse) { 35 if (s.currentTop !== null) { 36 s.stickyElement 37 .css('position', '') 38 .css('top', '') 39 .removeClass(s.className); 40 s.stickyElement.parent().removeClass(s.className); 41 s.currentTop = null; 42 } 43 } 44 else { 45 var newTop = documentHeight - s.stickyElement.outerHeight() 46 - s.topSpacing - s.bottomSpacing - scrollTop - extra; 47 if (newTop < 0) { 48 newTop = newTop + s.topSpacing; 49 } else { 50 newTop = s.topSpacing; 51 } 52 if (s.currentTop != newTop) { 53 s.stickyElement 54 .css('position', 'fixed') 55 .css('top', newTop) 56 .addClass(s.className); 57 s.stickyElement.parent().addClass(s.className); 58 s.currentTop = newTop; 59 } 60 } 61 } 62 }, 63 resizer = function() { 64 windowHeight = $window.height(); 65 }, 66 methods = { 67 init: function(options) { 68 var o = $.extend(defaults, options); 69 return this.each(function() { 70 var stickyElement = $(this); 71 72 stickyId = stickyElement.attr('id'); 73 74 // console.log(stickyId); 75 76 wrapper = $('<div></div>') 77 .attr('id', stickyId + '-sticky-wrapper') 78 .addClass(o.wrapperClassName); 79 stickyElement.wrapAll(wrapper); 80 var stickyWrapper = stickyElement.parent(); 81 stickyWrapper.css('height', stickyElement.outerHeight()); 82 sticked.push({ 83 topSpacing: o.topSpacing, 84 bottomSpacing: o.bottomSpacing, 85 stickyElement: stickyElement, 86 currentTop: null, 87 stickyWrapper: stickyWrapper, 88 className: o.className 89 }); 90 }); 91 }, 92 update: scroller 93 }; 94 95 // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): 96 if (window.addEventListener) { 97 window.addEventListener('scroll', scroller, false); 98 window.addEventListener('resize', resizer, false); 99 } else if (window.attachEvent) { 100 window.attachEvent('onscroll', scroller); 101 window.attachEvent('onresize', resizer); 102 } 103 104 $.fn.sticky = function(method) { 105 if (methods[method]) { 106 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 107 } else if (typeof method === 'object' || !method ) { 108 return methods.init.apply( this, arguments ); 109 } else { 110 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 111 } 112 }; 113 $(function() { 114 setTimeout(scroller, 0); 115 });
116})(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.