relatively-sticky

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

commit
b014e402045409eb35abddf70656d73350871f19
parent
24874c2d465e471b5ceadb302c14477ded219ec2
Author
John Chadwick <johnwchadwick@gmail.com>
Date
2014-11-21 00:03
Fix interplay between offset_top and inner_scrolling.

Diffstat

M jquery.sticky-kit.coffee 2 +-
M jquery.sticky-kit.js 4 ++--
M jquery.sticky-kit.min.js 7 +------

3 files changed, 4 insertions, 9 deletions


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

@@ -138,7 +138,7 @@ $.fn.stick_in_parent = (opts={}) ->
  138   138           # updated offset
  139   139           if inner_scrolling
  140   140             win_height = win.height()
  141    -1             if height > win_height # bigger than viewport
   -1   141             if height + offset_top > win_height # bigger than viewport
  142   142               unless bottomed
  143   143                 offset -= delta
  144   144                 offset = Math.max win_height - height, offset

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

@@ -1,4 +1,4 @@
    1    -1 // Generated by CoffeeScript 1.7.1
   -1     1 // Generated by CoffeeScript 1.8.0
    2     2 
    3     3 /**
    4     4 @license Sticky-kit v1.1.0 | WTFPL | Leaf Corcoran 2014 | http://leafo.net
@@ -146,7 +146,7 @@
  146   146           }
  147   147           if (inner_scrolling) {
  148   148             win_height = win.height();
  149    -1             if (height > win_height) {
   -1   149             if (height + offset_top > win_height) {
  150   150               if (!bottomed) {
  151   151                 offset -= delta;
  152   152                 offset = Math.max(win_height - height, offset);

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

@@ -1,9 +1,4 @@
    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,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";
    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;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),
    6    -1 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")),
    7    -1 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")},
    8    -1 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,
    9    -1 0)}};p=0;for(H=this.length;p<H;p++)d=this[p],G(k(d));return this}}).call(this);
   -1     4 (function(){var $,win;$=this.jQuery||window.jQuery;win=$(window);$.fn.stick_in_parent=function(opts){var elm,enable_bottoming,inner_scrolling,manual_spacer,offset_top,parent_selector,recalc_every,sticky_class,_fn,_i,_len;if(opts==null){opts={}}sticky_class=opts.sticky_class,inner_scrolling=opts.inner_scrolling,recalc_every=opts.recalc_every,parent_selector=opts.parent,offset_top=opts.offset_top,manual_spacer=opts.spacer,enable_bottoming=opts.bottoming;if(offset_top==null){offset_top=0}if(parent_selector==null){parent_selector=void 0}if(inner_scrolling==null){inner_scrolling=true}if(sticky_class==null){sticky_class="is_stuck"}if(enable_bottoming==null){enable_bottoming=true}_fn=function(elm,padding_bottom,parent_top,parent_height,top,height,el_float,detached){var bottomed,detach,fixed,last_pos,offset,parent,recalc,recalc_and_tick,recalc_counter,spacer,tick;if(elm.data("sticky_kit")){return}elm.data("sticky_kit",true);parent=elm.parent();if(parent_selector!=null){parent=parent.closest(parent_selector)}if(!parent.length){throw"failed to find stick parent"}fixed=false;bottomed=false;spacer=manual_spacer!=null?manual_spacer&&elm.closest(manual_spacer):$("<div />");if(spacer){spacer.css("position",elm.css("position"))}recalc=function(){var border_top,padding_top,restore;if(detached){return}border_top=parseInt(parent.css("border-top-width"),10);padding_top=parseInt(parent.css("padding-top"),10);padding_bottom=parseInt(parent.css("padding-bottom"),10);parent_top=parent.offset().top+border_top+padding_top;parent_height=parent.height();if(fixed){fixed=false;bottomed=false;if(manual_spacer==null){elm.insertAfter(spacer);spacer.detach()}elm.css({position:"",top:"",width:"",bottom:""}).removeClass(sticky_class);restore=true}top=elm.offset().top-parseInt(elm.css("margin-top"),10)-offset_top;height=elm.outerHeight(true);el_float=elm.css("float");if(spacer){spacer.css({width:elm.outerWidth(true),height:height,display:elm.css("display"),"vertical-align":elm.css("vertical-align"),"float":el_float})}if(restore){return tick()}};recalc();if(height===parent_height){return}last_pos=void 0;offset=offset_top;recalc_counter=recalc_every;tick=function(){var css,delta,scroll,will_bottom,win_height;if(detached){return}if(recalc_counter!=null){recalc_counter-=1;if(recalc_counter<=0){recalc_counter=recalc_every;recalc()}}scroll=win.scrollTop();if(last_pos!=null){delta=scroll-last_pos}last_pos=scroll;if(fixed){if(enable_bottoming){will_bottom=scroll+height+offset>parent_height+parent_top;if(bottomed&&!will_bottom){bottomed=false;elm.css({position:"fixed",bottom:"",top:offset}).trigger("sticky_kit:unbottom")}}if(scroll<top){fixed=false;offset=offset_top;if(manual_spacer==null){if(el_float==="left"||el_float==="right"){elm.insertAfter(spacer)}spacer.detach()}css={position:"",width:"",top:""};elm.css(css).removeClass(sticky_class).trigger("sticky_kit:unstick")}if(inner_scrolling){win_height=win.height();if(height+offset_top>win_height){if(!bottomed){offset-=delta;offset=Math.max(win_height-height,offset);offset=Math.min(offset_top,offset);if(fixed){elm.css({top:offset+"px"})}}}}}else{if(scroll>top){fixed=true;css={position:"fixed",top:offset};css.width=elm.css("box-sizing")==="border-box"?elm.outerWidth()+"px":elm.width()+"px";elm.css(css).addClass(sticky_class);if(manual_spacer==null){elm.after(spacer);if(el_float==="left"||el_float==="right"){spacer.append(elm)}}elm.trigger("sticky_kit:stick")}}if(fixed&&enable_bottoming){if(will_bottom==null){will_bottom=scroll+height+offset>parent_height+parent_top}if(!bottomed&&will_bottom){bottomed=true;if(parent.css("position")==="static"){parent.css({position:"relative"})}return elm.css({position:"absolute",bottom:padding_bottom,top:"auto"}).trigger("sticky_kit:bottom")}}};recalc_and_tick=function(){recalc();return tick()};detach=function(){detached=true;win.off("touchmove",tick);win.off("scroll",tick);win.off("resize",recalc_and_tick);$(document.body).off("sticky_kit:recalc",recalc_and_tick);elm.off("sticky_kit:detach",detach);elm.removeData("sticky_kit");elm.css({position:"",bottom:"",top:"",width:""});parent.position("position","");if(fixed){if(manual_spacer==null){if(el_float==="left"||el_float==="right"){elm.insertAfter(spacer)}spacer.remove()}return elm.removeClass(sticky_class)}};win.on("touchmove",tick);win.on("scroll",tick);win.on("resize",recalc_and_tick);$(document.body).on("sticky_kit:recalc",recalc_and_tick);elm.on("sticky_kit:detach",detach);return setTimeout(tick,0)};for(_i=0,_len=this.length;_i<_len;_i++){elm=this[_i];_fn($(elm))}return this}}).call(this);