//call sticky function jquery(document).ready(function($){ $("#sticky").sticky({topspacing:0}); }); // sticky plugin v1.0.2 for jquery // ============= // author: anthony garand // improvements by german m. bravo (kronuz) and ruud kamphuis (ruudk) // improvements by leonardo c. daronco (daronco) // created: 2/14/2011 // date: 16/04/2015 // website: http://labs.anthonygarand.com/sticky // description: makes an element on the page stick on the screen as you scroll // it will only set the 'top' and 'position' of your element, you // might need to adjust the width in some cases. (function($) { var slice = array.prototype.slice; // save ref to original slice() var splice = array.prototype.splice; // save ref to original slice() var defaults = { topspacing: 0, bottomspacing: 0, classname: 'is-sticky', wrapperclassname: 'sticky-wrapper', center: false, getwidthfrom: '', widthfromwrapper: true, // works only when .getwidthfrom is empty responsivewidth: false }, $window = $(window), $document = $(document), sticked = [], windowheight = $window.height(), scroller = function() { var scrolltop = $window.scrolltop(), documentheight = $document.height(), dwh = documentheight - windowheight, extra = (scrolltop > dwh) ? dwh - scrolltop : 0; for (var i = 0; i < sticked.length; i++) { var s = sticked[i], elementtop = s.stickywrapper.offset().top, etse = elementtop - s.topspacing - extra; if (scrolltop <= etse) { if (s.currenttop !== null) { s.stickyelement .css({ 'width': '', 'position': '', 'top': '' }); s.stickyelement.parent().removeclass(s.classname); s.stickyelement.trigger('sticky-end', [s]); s.currenttop = null; } } else { var newtop = documentheight - s.stickyelement.outerheight() - s.topspacing - s.bottomspacing - scrolltop - extra; if (newtop < 0) { newtop = newtop + s.topspacing; } else { newtop = s.topspacing; } if (s.currenttop != newtop) { var newwidth; if ( s.getwidthfrom ) { newwidth = $(s.getwidthfrom).width() || null; } else if(s.widthfromwrapper) { newwidth = s.stickywrapper.width(); } if ( newwidth == null ) { newwidth = s.stickyelement.width(); } s.stickyelement .css('width', newwidth) .css('position', 'fixed') .css('top', newtop); s.stickyelement.parent().addclass(s.classname); if (s.currenttop === null) { s.stickyelement.trigger('sticky-start', [s]); } else { // sticky is started but it have to be repositioned s.stickyelement.trigger('sticky-update', [s]); } if (s.currenttop === s.topspacing && s.currenttop > newtop || s.currenttop === null && newtop < s.topspacing) { // just reached bottom || just started to stick but bottom is already reached s.stickyelement.trigger('sticky-bottom-reached', [s]); } else if(s.currenttop !== null && newtop === s.topspacing && s.currenttop < newtop) { // sticky is started && sticked at topspacing && overflowing from top just finished s.stickyelement.trigger('sticky-bottom-unreached', [s]); } s.currenttop = newtop; } } } }, resizer = function() { windowheight = $window.height(); for (var i = 0; i < sticked.length; i++) { var s = sticked[i]; var newwidth = null; if ( s.getwidthfrom ) { if ( s.responsivewidth === true ) { newwidth = $(s.getwidthfrom).width(); } } else if(s.widthfromwrapper) { newwidth = s.stickywrapper.width(); } if ( newwidth != null ) { s.stickyelement.css('width', newwidth); } } }, methods = { init: function(options) { var o = $.extend({}, defaults, options); return this.each(function() { var stickyelement = $(this); var stickyid = stickyelement.attr('id'); var stickyheight = stickyelement.outerheight(); var wrapperid = stickyid ? stickyid + '-' + defaults.wrapperclassname : defaults.wrapperclassname var wrapper = $('
') .attr('id', wrapperid) .addclass(o.wrapperclassname); stickyelement.wrapall(wrapper); var stickywrapper = stickyelement.parent(); if (o.center) { stickywrapper.css({width:stickyelement.outerwidth(),marginleft:"auto",marginright:"auto"}); } if (stickyelement.css("float") == "right") { stickyelement.css({"float":"none"}).parent().css({"float":"right"}); } stickywrapper.css('height', stickyheight); o.stickyelement = stickyelement; o.stickywrapper = stickywrapper; o.currenttop = null; sticked.push(o); }); }, update: scroller, unstick: function(options) { return this.each(function() { var that = this; var unstickyelement = $(that); var removeidx = -1; var i = sticked.length; while ( i-- > 0 ) { if (sticked[i].stickyelement.get(0) === that) { splice.call(sticked,i,1); removeidx = i; } } if(removeidx != -1) { unstickyelement.unwrap(); unstickyelement .css({ 'width': '', 'position': '', 'top': '', 'float': '' }) ; } }); } }; // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): if (window.addeventlistener) { window.addeventlistener('scroll', scroller, false); window.addeventlistener('resize', resizer, false); } else if (window.attachevent) { window.attachevent('onscroll', scroller); window.attachevent('onresize', resizer); } $.fn.sticky = function(method) { if (methods[method]) { return methods[method].apply(this, slice.call(arguments, 1)); } else if (typeof method === 'object' || !method ) { return methods.init.apply( this, arguments ); } else { $.error('method ' + method + ' does not exist on jquery.sticky'); } }; $.fn.unstick = function(method) { if (methods[method]) { return methods[method].apply(this, slice.call(arguments, 1)); } else if (typeof method === 'object' || !method ) { return methods.unstick.apply( this, arguments ); } else { $.error('method ' + method + ' does not exist on jquery.sticky'); } }; $(function() { settimeout(scroller, 0); }); })(jquery);