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/bower.json b/bower.json new file mode 100644 index 0000000..1d775ac --- /dev/null +++ b/bower.json @@ -0,0 +1,25 @@ +{ + "name": "jquery.pin", + "main": "jquery.pin.js", + "version": "1.0.4", + "homepage": "http://webpop.github.io/jquery.pin/", + "authors": [ + "Mathias Biilmann " + ], + "dependencies": { + "jquery": ">=1.8.0" + }, + "description": "jQuery.Pin is a plugin to pin any element within a container", + "keywords": [ + "pin", + "sticky" + ], + "license": "The BSD 2-Clause License", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/jquery.pin.js b/jquery.pin.js index 7e43198..97d25c8 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 }); @@ -56,8 +62,8 @@ elmts.push($this); - var from = data.from, - to = data.to; + var from = data.from - data.pad.bottom, + to = data.to - data.pad.top; if (from + $this.outerHeight() > data.end) { $this.css('position', ''); @@ -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..04cc68d 100644 --- a/jquery.pin.min.js +++ b/jquery.pin.min.js @@ -1 +1 @@ -(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){"use strict";e.fn.pin=function(t){var n=0,r=[],i=false,s=e(window);t=t||{};var o=function(){for(var n=0,o=r.length;n")}var h=e.extend({top:0,bottom:0},t.padding||{});u.data("pin",{pad:h,from:(t.containerSelector?l.top:f.top)-h.top,to:l.top+a.height()-u.outerHeight()-h.bottom,end:l.top+a.height(),parentTop:c.top});u.css({width:u.outerWidth()});u.parent().css("height",u.outerHeight())}};var u=function(){if(i){return}n=s.scrollTop();var o=[];for(var u=0,a=r.length;ul.end){f.css("position","");continue}if(cn){!(f.css("position")=="fixed")&&f.css({left:f.offset().left,top:l.pad.top}).css("position","fixed");if(t.activeClass){f.addClass(t.activeClass)}}else if(n>=h){f.css({left:"",top:h-l.parentTop+l.pad.top}).css("position","absolute");if(t.activeClass){f.addClass(t.activeClass)}}else{f.css({position:"",top:"",left:""});if(t.activeClass){f.removeClass(t.activeClass)}}}r=o};var a=function(){o();u()};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",o);n.update=a;e(this).data("pin",n)});s.scroll(u);s.resize(function(){o()});o();s.load(a);return this}})(jQuery) \ No newline at end of file