1// 6. 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 'use strict'; 15 var defaults = { 16 topSpacing: 0, 17 bottomSpacing: 0, 18 className: 'is-sticky', 19 wrapperClassName: 'sticky-wrapper', 20 center: false, 21 getWidthFrom: '' 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.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.parent().addClass(s.className); 65 s.currentTop = newTop; 66 } 67 } 68 } 69 }, 70 resizer = function() { 71 windowHeight = $window.height(); 72 }, 73 methods = { 74 init: function(options) { 75 var o = $.extend(defaults, options); 76 return this.each(function() { 77 var stickyElement = $(this); 78 79 var stickyId = stickyElement.attr('id'); 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 } 88 89 if (stickyElement.css("float") == "right") { 90 stickyElement.css({"float":"none"}).parent().css({"float":"right"}); 91 } 92 93 var stickyWrapper = stickyElement.parent(); 94 stickyWrapper.css('height', stickyElement.outerHeight()); 95 sticked.push({ 96 topSpacing: o.topSpacing, 97 bottomSpacing: o.bottomSpacing, 98 stickyElement: stickyElement, 99 currentTop: null, 100 stickyWrapper: stickyWrapper, 101 className: o.className, 102 getWidthFrom: o.getWidthFrom 103 }); 104 }); 105 }, 106 update: scroller 107 }; 108 109 if (window.addEventListener) { 110 window.addEventListener('scroll', scroller, false); 111 window.addEventListener('resize', resizer, false); 112 } else if (window.attachEvent) { 113 window.attachEvent('onscroll', scroller); 114 window.attachEvent('onresize', resizer); 115 } 116 117 $.fn.sticky = function(method) { 118 if (methods[method]) { 119 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 120 } else if (typeof method === 'object' || !method ) { 121 return methods.init.apply( this, arguments ); 122 } else { 123 $.error('Method ' + method + ' does not exist on jQuery.sticky'); 124 } 125 }; 126 $(function() { 127 setTimeout(scroller, 0); 128 }); 129})(jQuery); 130
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.