relatively-sticky

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

commit
bfeccbd48c7b748440dda2ac1aee6601736bab65
parent
252e56e00337f3d8564f21b1984229bec91d4f7b
Author
leaf corcoran <leafot@gmail.com>
Date
2013-09-11 16:59
add offset parameter #1 #2

Diffstat

M jquery.sticky-kit.coffee 14 +++++++-------
M jquery.sticky-kit.js 20 +++++++++++---------

2 files changed, 18 insertions, 16 deletions


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

@@ -6,7 +6,8 @@ $ = @jQuery
    6     6 
    7     7 win = $ window
    8     8 $.fn.stick_in_parent = (opts={}) ->
    9    -1   { sticky_class, inner_scrolling, parent: parent_selector } = opts
   -1     9   { sticky_class, inner_scrolling, parent: parent_selector, offset_top } = opts
   -1    10   offset_top ?= 0
   10    11   parent_selector ?= undefined
   11    12   inner_scrolling ?= true
   12    13   sticky_class ?= "is_stuck"
@@ -30,7 +31,7 @@ $.fn.stick_in_parent = (opts={}) ->
   30    31         else
   31    32           elm
   32    33 
   33    -1         top = sizing_elm.offset().top - parseInt sizing_elm.css("margin-top"), 10
   -1    34         top = sizing_elm.offset().top - parseInt(sizing_elm.css("margin-top"), 10) - offset_top
   34    35         height = sizing_elm.outerHeight true
   35    36 
   36    37       recalc()
@@ -50,7 +51,7 @@ $.fn.stick_in_parent = (opts={}) ->
   50    51       fixed = false
   51    52       bottomed = false
   52    53       last_pos = undefined
   53    -1       offset = 0
   -1    54       offset = offset_top
   54    55       reset_width = false
   55    56 
   56    57       tick = ->
@@ -68,13 +69,13 @@ $.fn.stick_in_parent = (opts={}) ->
   68    69             elm.css({
   69    70               position: "fixed"
   70    71               bottom: ""
   71    -1               top: 0
   -1    72               top: offset
   72    73             }).trigger("sticky_kit:unbottom")
   73    74 
   74    75           # unfixing
   75    76           if scroll < top
   76    77             fixed = false
   77    -1             offset = 0
   -1    78             offset = offset_top
   78    79 
   79    80             if float == "left" || float == "right"
   80    81               elm.insertAfter spacer
@@ -92,9 +93,8 @@ $.fn.stick_in_parent = (opts={}) ->
   92    93             if height > win_height # bigger than viewport
   93    94               unless bottomed
   94    95                 offset -= delta
   95    -1                 before = offset
   96    96                 offset = Math.max win_height - height, offset
   97    -1                 offset = Math.min 0, offset
   -1    97                 offset = Math.min offset_top, offset
   98    98 
   99    99                 elm.css {
  100   100                   top: offset + "px"

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

@@ -13,11 +13,14 @@
   13    13   win = $(window);
   14    14 
   15    15   $.fn.stick_in_parent = function(opts) {
   16    -1     var elm, inner_scrolling, parent_selector, sticky_class, _fn, _i, _len;
   -1    16     var elm, inner_scrolling, offset_top, parent_selector, sticky_class, _fn, _i, _len;
   17    17     if (opts == null) {
   18    18       opts = {};
   19    19     }
   20    -1     sticky_class = opts.sticky_class, inner_scrolling = opts.inner_scrolling, parent_selector = opts.parent;
   -1    20     sticky_class = opts.sticky_class, inner_scrolling = opts.inner_scrolling, parent_selector = opts.parent, offset_top = opts.offset_top;
   -1    21     if (offset_top == null) {
   -1    22       offset_top = 0;
   -1    23     }
   21    24     if (parent_selector == null) {
   22    25       parent_selector = void 0;
   23    26     }
@@ -44,7 +47,7 @@
   44    47         parent_top = parent.offset().top + border_top + padding_top;
   45    48         parent_height = parent.height();
   46    49         sizing_elm = elm.is(".is_stuck") ? spacer : elm;
   47    -1         top = sizing_elm.offset().top - parseInt(sizing_elm.css("margin-top"), 10);
   -1    50         top = sizing_elm.offset().top - parseInt(sizing_elm.css("margin-top"), 10) - offset_top;
   48    51         return height = sizing_elm.outerHeight(true);
   49    52       };
   50    53       recalc();
@@ -62,10 +65,10 @@
   62    65       fixed = false;
   63    66       bottomed = false;
   64    67       last_pos = void 0;
   65    -1       offset = 0;
   -1    68       offset = offset_top;
   66    69       reset_width = false;
   67    70       tick = function() {
   68    -1         var before, css, delta, scroll, will_bottom, win_height;
   -1    71         var css, delta, scroll, will_bottom, win_height;
   69    72         scroll = win.scrollTop();
   70    73         if (last_pos != null) {
   71    74           delta = scroll - last_pos;
@@ -78,12 +81,12 @@
   78    81             elm.css({
   79    82               position: "fixed",
   80    83               bottom: "",
   81    -1               top: 0
   -1    84               top: offset
   82    85             }).trigger("sticky_kit:unbottom");
   83    86           }
   84    87           if (scroll < top) {
   85    88             fixed = false;
   86    -1             offset = 0;
   -1    89             offset = offset_top;
   87    90             if (float === "left" || float === "right") {
   88    91               elm.insertAfter(spacer);
   89    92             }
@@ -101,9 +104,8 @@
  101   104             if (height > win_height) {
  102   105               if (!bottomed) {
  103   106                 offset -= delta;
  104    -1                 before = offset;
  105   107                 offset = Math.max(win_height - height, offset);
  106    -1                 offset = Math.min(0, offset);
   -1   108                 offset = Math.min(offset_top, offset);
  107   109                 elm.css({
  108   110                   top: offset + "px"
  109   111                 });