From 5567616e41c4afcf868031392a6d7e9044355908 Mon Sep 17 00:00:00 2001 From: David Wolever Date: Sun, 29 Dec 2013 20:13:28 -0500 Subject: [PATCH] Add 'padding' option --- README.md | 6 +++++- jquery.pin.js | 14 ++++++++++---- jquery.pin.min.js | 5 ++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 65afee3..f900047 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ To make a pinned element stay within an outer container, use the containerSelect $(".pinned").pin({containerSelector: ".container"}) +Padding can also be added around the pinned element while scrolling: + + $(".pinned").pin({padding: {top: 10, bottom: 10}}) + That's it - go pin all the things! ## Examples @@ -29,4 +33,4 @@ Redistribution and use in source and binary forms, with or without modification, Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/jquery.pin.js b/jquery.pin.js index 7e43198..30aa09a 100644 --- a/jquery.pin.js +++ b/jquery.pin.js @@ -28,9 +28,15 @@ $this.wrap("
"); } + var pad = $.extend({ + top: 0, + bottom: 0 + }, options.padding || {}); + $this.data("pin", { - from: options.containerSelector ? containerOffset.top : offset.top, - to: containerOffset.top + $container.height() - $this.outerHeight(), + 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 }); @@ -67,13 +73,13 @@ if (from < scrollY && to > scrollY) { !($this.css("position") == "fixed") && $this.css({ left: $this.offset().left, - top: 0 + 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 + top: to - data.parentTop + data.pad.top }).css("position", "absolute"); if (options.activeClass) { $this.addClass(options.activeClass); } } else { diff --git a/jquery.pin.min.js b/jquery.pin.min.js index fd93583..f22dcea 100644 --- a/jquery.pin.min.js +++ b/jquery.pin.min.js @@ -1 +1,4 @@ -(function(e){"use strict";e.fn.pin=function(t){var n=0,r=[],i=!1;t=t||{};var s=function(){for(var n=0,s=r.length;n"),o.data("pin",{from:t.containerSelector?f.top:a.top,to:f.top+u.height()-o.outerHeight(),end:f.top+u.height(),parentTop:l.top}),o.css({width:o.outerWidth()}),o.parent().css("height",o.outerHeight())}},o=function(){if(i)return;n=window.scrollY;for(var t=0,s=r.length;tu.end){o.css("position","");continue}an?o.css("position")!="fixed"&&o.css({left:o.offset().left,top:0}).css("position","fixed"):n>=f?o.css({left:"",top:f-u.parentTop}).css("position","absolute"):o.css({position:"",top:"",left:""})}},u=function(){s(),o()};return this.each(function(){var t=e(this),n=e(this).data("pin")||{};if(n&&n.update)return;r.push(t),e("img",this).one("load",s),n.update=u,e(this).data("pin",n)}),e(window).scroll(o),e(window).resize(function(){s()}),s(),e(window).load(u),this}})(jQuery); \ No newline at end of file +(function(e){e.fn.pin=function(a){var g=0,h=[],n=!1,k=e(window),a=a||{},m=function(){for(var i=0,d=h.length;i"); +var g=e.extend({top:0,bottom:0},a.padding||{});b.data("pin",{pad:g,from:(a.containerSelector?j.top:f.top)-g.top,to:j.top+c.height()-b.outerHeight()-g.bottom,end:j.top+c.height(),parentTop:l.top});b.css({width:b.outerWidth()});b.parent().css("height",b.outerHeight())}}},o=function(){if(!n){g=k.scrollTop();for(var i=[],d=0,b=h.length;df.end?c.css("position",""):jg?("fixed"!=c.css("position")&&c.css({left:c.offset().left, +top:f.pad.top}).css("position","fixed"),a.activeClass&&c.addClass(a.activeClass)):g>=l?(c.css({left:"",top:l-f.parentTop+f.pad.top}).css("position","absolute"),a.activeClass&&c.addClass(a.activeClass)):(c.css({position:"",top:"",left:""}),a.activeClass&&c.removeClass(a.activeClass))}}h=i}},p=function(){m();o()};this.each(function(){var a=e(this),d=e(this).data("pin")||{};if(!d||!d.update)h.push(a),e("img",this).one("load",m),d.update=p,e(this).data("pin",d)});k.scroll(o);k.resize(function(){m()}); +m();k.load(p);return this}})(jQuery);