relatively-sticky

A jQuery plugin for creating smart sticky elements
git clone https://git.ce9e.org/relatively-sticky.git

commit
4f26d87bb2b1708e8581c5581b2d32db875a6adc
parent
85a02263cfe5ced9ce56bc02b360414bf361d750
Author
leaf corcoran <leafot@gmail.com>
Date
2013-08-01 04:07
use element top instead of parent top to initiate stick

Diffstat

M jquery.sticky-kit.coffee 7 ++++---
M jquery.sticky-kit.js 7 ++++---
M jquery.sticky-kit.min.js 8 ++++----

3 files changed, 12 insertions, 10 deletions


diff --git a/jquery.sticky-kit.coffee b/jquery.sticky-kit.coffee

@@ -12,7 +12,7 @@ $.fn.stick_in_parent = (opts={}) ->
   12    12   sticky_class ?= "is_stuck"
   13    13 
   14    14   for elm in @
   15    -1     ((elm, padding_bottom, parent_top, parent_height, height) ->
   -1    15     ((elm, padding_bottom, parent_top, parent_height, top, height) ->
   16    16       parent = elm.parent()
   17    17       parent = parent.closest(parent_selector) if parent_selector?
   18    18       throw "failed to find stick parent" unless parent.length
@@ -25,6 +25,7 @@ $.fn.stick_in_parent = (opts={}) ->
   25    25         parent_top = parent.offset().top + border_top + padding_top
   26    26         parent_height = parent.height()
   27    27 
   -1    28         top = elm.offset().top - parseInt elm.css("margin-top"), 10
   28    29         height = elm.outerHeight true
   29    30 
   30    31       recalc()
@@ -65,7 +66,7 @@ $.fn.stick_in_parent = (opts={}) ->
   65    66             }).trigger("sticky_kit:unbottom")
   66    67 
   67    68           # unfixing
   68    -1           if scroll < parent_top
   -1    69           if scroll < top
   69    70             fixed = false
   70    71             offset = 0
   71    72 
@@ -95,7 +96,7 @@ $.fn.stick_in_parent = (opts={}) ->
   95    96 
   96    97         else
   97    98           # fixing
   98    -1           if scroll > parent_top
   -1    99           if scroll > top
   99   100             fixed = true
  100   101             css = {
  101   102               position: "fixed"

diff --git a/jquery.sticky-kit.js b/jquery.sticky-kit.js

@@ -27,7 +27,7 @@
   27    27     if (sticky_class == null) {
   28    28       sticky_class = "is_stuck";
   29    29     }
   30    -1     _fn = function(elm, padding_bottom, parent_top, parent_height, height) {
   -1    30     _fn = function(elm, padding_bottom, parent_top, parent_height, top, height) {
   31    31       var bottomed, fixed, float, last_pos, offset, parent, recalc, reset_width, spacer, tick;
   32    32       parent = elm.parent();
   33    33       if (parent_selector != null) {
@@ -43,6 +43,7 @@
   43    43         padding_bottom = parseInt(parent.css("padding-bottom"), 10);
   44    44         parent_top = parent.offset().top + border_top + padding_top;
   45    45         parent_height = parent.height();
   -1    46         top = elm.offset().top - parseInt(elm.css("margin-top"), 10);
   46    47         return height = elm.outerHeight(true);
   47    48       };
   48    49       recalc();
@@ -78,7 +79,7 @@
   78    79               top: 0
   79    80             }).trigger("sticky_kit:unbottom");
   80    81           }
   81    -1           if (scroll < parent_top) {
   -1    82           if (scroll < top) {
   82    83             fixed = false;
   83    84             offset = 0;
   84    85             if (float === "left" || float === "right") {
@@ -108,7 +109,7 @@
  108   109             }
  109   110           }
  110   111         } else {
  111    -1           if (scroll > parent_top) {
   -1   112           if (scroll > top) {
  112   113             fixed = true;
  113   114             css = {
  114   115               position: "fixed",

diff --git a/jquery.sticky-kit.min.js b/jquery.sticky-kit.min.js

@@ -1,7 +1,7 @@
    1     1 /*
    2     2  WTFPL | Leaf Corcoran 2013 | http://leafo.net
    3     3 */
    4    -1 (function(){var g,t;g=this.jQuery;t=g(window);g.fn.stick_in_parent=function(e){var u,c,p,z,h,A;null==e&&(e={});p=e.sticky_class;u=e.inner_scrolling;c=e.parent;null==c&&(c=void 0);null==u&&(u=!0);null==p&&(p="is_stuck");z=function(a,e,q,h,l){var n,r,f,v,b,d,w,x,s,y;d=a.parent();null!=c&&(d=d.closest(c));if(!d.length)throw"failed to find stick parent";w=function(){var b,f;b=parseInt(d.css("border-top-width"),10);f=parseInt(d.css("padding-top"),10);e=parseInt(d.css("padding-bottom"),10);q=d.offset().top+
    5    -1 b+f;h=d.height();return l=a.outerHeight(!0)};w();if(l!==h)return f=a.css("float"),s=g("<div />").css({width:a.outerWidth(!0),height:l,display:a.css("display"),"float":f}),n=r=!1,v=void 0,b=0,x=!1,y=function(){var k,g,m,c;m=t.scrollTop();null!=v&&(g=m-v);v=m;r?(c=m+l+b>h+q,n&&!c&&(n=!1,a.css({position:"fixed",bottom:"",top:0}).trigger("sticky_kit:unbottom")),m<q&&(r=!1,b=0,"left"!==f&&"right"!==f||a.insertAfter(s),s.detach(),k={position:""},x&&(k.width=""),a.css(k).removeClass(p).trigger("sticky_kit:unstick")),
    6    -1 u&&(k=t.height(),l>k&&!n&&(b-=g,b=Math.max(k-l,b),b=Math.min(0,b),a.css({top:b+"px"})))):m>q&&(r=!0,k={position:"fixed",top:b},"none"===f&&"block"===a.css("display")&&(k.width=a.width()+"px",x=!0),a.css(k).addClass(p).after(s),"left"!==f&&"right"!==f||s.append(a),a.trigger("sticky_kit:stick"));if(r&&(null==c&&(c=m+l+b>h+q),!n&&c))return n=!0,"static"===d.css("position")&&d.css({position:"relative"}),a.css({position:"absolute",bottom:e,top:""}).trigger("sticky_kit:bottom")},t.on("scroll",y),g(document.body).on("sticky_kit:recalc",
    7    -1 function(){w();return y()})};h=0;for(A=this.length;h<A;h++)e=this[h],z(g(e));return this}}).call(this);
   -1     4 (function(){var g,s;g=this.jQuery;s=g(window);g.fn.stick_in_parent=function(e){var t,c,p,A,k,B;null==e&&(e={});p=e.sticky_class;t=e.inner_scrolling;c=e.parent;null==c&&(c=void 0);null==t&&(t=!0);null==p&&(p="is_stuck");A=function(a,e,k,u,w,l){var n,q,f,v,b,d,x,y,r,z;d=a.parent();null!=c&&(d=d.closest(c));if(!d.length)throw"failed to find stick parent";x=function(){var b,f;b=parseInt(d.css("border-top-width"),10);f=parseInt(d.css("padding-top"),10);e=parseInt(d.css("padding-bottom"),10);k=d.offset().top+
   -1     5 b+f;u=d.height();w=a.offset().top-parseInt(a.css("margin-top"),10);return l=a.outerHeight(!0)};x();if(l!==u)return f=a.css("float"),r=g("<div />").css({width:a.outerWidth(!0),height:l,display:a.css("display"),"float":f}),n=q=!1,v=void 0,b=0,y=!1,z=function(){var h,g,m,c;m=s.scrollTop();null!=v&&(g=m-v);v=m;q?(c=m+l+b>u+k,n&&!c&&(n=!1,a.css({position:"fixed",bottom:"",top:0}).trigger("sticky_kit:unbottom")),m<w&&(q=!1,b=0,"left"!==f&&"right"!==f||a.insertAfter(r),r.detach(),h={position:""},y&&(h.width=
   -1     6 ""),a.css(h).removeClass(p).trigger("sticky_kit:unstick")),t&&(h=s.height(),l>h&&!n&&(b-=g,b=Math.max(h-l,b),b=Math.min(0,b),a.css({top:b+"px"})))):m>w&&(q=!0,h={position:"fixed",top:b},"none"===f&&"block"===a.css("display")&&(h.width=a.width()+"px",y=!0),a.css(h).addClass(p).after(r),"left"!==f&&"right"!==f||r.append(a),a.trigger("sticky_kit:stick"));if(q&&(null==c&&(c=m+l+b>u+k),!n&&c))return n=!0,"static"===d.css("position")&&d.css({position:"relative"}),a.css({position:"absolute",bottom:e,top:""}).trigger("sticky_kit:bottom")},
   -1     7 s.on("scroll",z),g(document.body).on("sticky_kit:recalc",function(){x();return z()})};k=0;for(B=this.length;k<B;k++)e=this[k],A(g(e));return this}}).call(this);