relatively-sticky

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

commit
24874c2d465e471b5ceadb302c14477ded219ec2
parent
a6a4ddcc897a8ce8a1f9daacfa82e8d2ab4741c0
Author
leaf corcoran <leafot@gmail.com>
Date
2014-11-20 08:34
don't let handlers run if element has been detached #60

Diffstat

M jquery.sticky-kit.coffee 5 ++++-
M jquery.sticky-kit.js 9 ++++++++-
M jquery.sticky-kit.min.js 12 ++++++------

3 files changed, 18 insertions, 8 deletions


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

@@ -24,7 +24,7 @@ $.fn.stick_in_parent = (opts={}) ->
   24    24   enable_bottoming = true unless enable_bottoming?
   25    25 
   26    26   for elm in @
   27    -1     ((elm, padding_bottom, parent_top, parent_height, top, height, el_float) ->
   -1    27     ((elm, padding_bottom, parent_top, parent_height, top, height, el_float, detached) ->
   28    28       return if elm.data "sticky_kit"
   29    29       elm.data "sticky_kit", true
   30    30 
@@ -42,6 +42,7 @@ $.fn.stick_in_parent = (opts={}) ->
   42    42       spacer.css('position', elm.css('position')) if spacer
   43    43 
   44    44       recalc = ->
   -1    45         return if detached
   45    46         border_top = parseInt parent.css("border-top-width"), 10
   46    47         padding_top = parseInt parent.css("padding-top"), 10
   47    48         padding_bottom = parseInt parent.css("padding-bottom"), 10
@@ -91,6 +92,7 @@ $.fn.stick_in_parent = (opts={}) ->
   91    92       recalc_counter = recalc_every
   92    93 
   93    94       tick = ->
   -1    95         return if detached
   94    96         if recalc_counter?
   95    97           recalc_counter -= 1
   96    98           if recalc_counter <= 0
@@ -196,6 +198,7 @@ $.fn.stick_in_parent = (opts={}) ->
  196   198         tick()
  197   199 
  198   200       detach = ->
   -1   201         detached = true
  199   202         win.off "touchmove", tick
  200   203         win.off "scroll", tick
  201   204         win.off "resize", recalc_and_tick

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

@@ -32,7 +32,7 @@
   32    32     if (enable_bottoming == null) {
   33    33       enable_bottoming = true;
   34    34     }
   35    -1     _fn = function(elm, padding_bottom, parent_top, parent_height, top, height, el_float) {
   -1    35     _fn = function(elm, padding_bottom, parent_top, parent_height, top, height, el_float, detached) {
   36    36       var bottomed, detach, fixed, last_pos, offset, parent, recalc, recalc_and_tick, recalc_counter, spacer, tick;
   37    37       if (elm.data("sticky_kit")) {
   38    38         return;
@@ -53,6 +53,9 @@
   53    53       }
   54    54       recalc = function() {
   55    55         var border_top, padding_top, restore;
   -1    56         if (detached) {
   -1    57           return;
   -1    58         }
   56    59         border_top = parseInt(parent.css("border-top-width"), 10);
   57    60         padding_top = parseInt(parent.css("padding-top"), 10);
   58    61         padding_bottom = parseInt(parent.css("padding-bottom"), 10);
@@ -98,6 +101,9 @@
   98   101       recalc_counter = recalc_every;
   99   102       tick = function() {
  100   103         var css, delta, scroll, will_bottom, win_height;
   -1   104         if (detached) {
   -1   105           return;
   -1   106         }
  101   107         if (recalc_counter != null) {
  102   108           recalc_counter -= 1;
  103   109           if (recalc_counter <= 0) {
@@ -195,6 +201,7 @@
  195   201         return tick();
  196   202       };
  197   203       detach = function() {
   -1   204         detached = true;
  198   205         win.off("touchmove", tick);
  199   206         win.off("scroll", tick);
  200   207         win.off("resize", recalc_and_tick);

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

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