(function( $ ) { // http://codedumper.com/azoku var win = $( window ), winTop, elems = [], bound = false; function normalize( val ) { return $.isFunction( val ) ? val() : val; } function position() { var currentTop = this.elem.offset().top, options = this.options, min = normalize( options.min ); if ( windowTop > min || currentTop > min ) { var top = Math.max( windowTop, min ); if ( options.max ) { top = Math.min( top, normalize(options.max) ); } this.elem.stop().animate({ top: top }, 200); } } $.fn.floatFixed = function( options ) { options = $.extend( { min: "auto" }, options ); // TODO: make this work with multiple elements if ( options.min === "auto" ) { options.min = self.offset().top; } this.each(function() { elems.push({ elem: $(this), options: options }); }); if ( !bound ) { win.scroll(function() { windowTop = win.scrollTop(); $.each( elems, position ); }); } return this; }; })( jQuery );