(function ($) { "use strict"; $.fn.pin = function (options) { var scrolly = 0, elements = [], disabled = false, $window = $(window); options = options || {}; var recalculatelimits = function () { for (var i=0, len=elements.length; i"); } var pad = $.extend({ top: 0, bottom: 0 }, options.padding || {}); $this.data("pin", { pad: pad, from: (options.containerselector ? containeroffset.top : offset.top) - pad.top, to: containeroffset.top + $container.height() - $this.outerheight() - pad.bottom, end: containeroffset.top + $container.height(), parenttop: parentoffset.top }); $this.css({width: $this.outerwidth()}); $this.parent().css("height", $this.outerheight()); } }; var onscroll = function () { if (disabled) { return; } scrolly = $window.scrolltop(); var elmts = []; for (var i=0, len=elements.length; i data.end) { $this.css('position', ''); continue; } if (from < scrolly && to > scrolly) { !($this.css("position") == "fixed") && $this.css({ left: $this.offset().left, top: data.pad.top }).css("position", "fixed"); if (options.activeclass) { $this.addclass(options.activeclass); } } else if (scrolly >= to) { $this.css({ left: "", top: to - data.parenttop + data.pad.top }).css("position", "absolute"); if (options.activeclass) { $this.addclass(options.activeclass); } } else { $this.css({position: "", top: "", left: ""}); if (options.activeclass) { $this.removeclass(options.activeclass); } } } elements = elmts; }; var update = function () { recalculatelimits(); onscroll(); }; this.each(function () { var $this = $(this), data = $(this).data('pin') || {}; if (data && data.update) { return; } elements.push($this); $("img", this).one("load", recalculatelimits); data.update = update; $(this).data('pin', data); }); $window.scroll(onscroll); $window.resize(function () { recalculatelimits(); }); recalculatelimits(); $window.on('load',update); return this; }; })(jquery);