diff --git a/dist/sticky-kit.js b/dist/sticky-kit.js index 00b1ea2..1f723ec 100644 --- a/dist/sticky-kit.js +++ b/dist/sticky-kit.js @@ -1,22 +1,26 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 /** -@license Sticky-kit v1.1.3 | MIT | Leaf Corcoran 2015 | http://leafo.net +@license Sticky-kit v1.1.4 | MIT | Leaf Corcoran 2015 | http://leafo.net */ (function() { - var $, win; + var $, doc, win; $ = window.jQuery; win = $(window); + doc = $(document); + $.fn.stick_in_parent = function(opts) { - var doc, elm, enable_bottoming, fn, i, inner_scrolling, len, manual_spacer, offset_top, outer_width, parent_selector, recalc_every, sticky_class; + var doc_height, elm, enable_bottoming, fn, i, inner_scrolling, len, manual_spacer, bar_height, offset_top, outer_width, parent_selector, recalc_every, ref, set_bottom, sticky_class, win_height; 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; + 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, set_bottom = opts.set_bottom, bar_height = opts.bar_height; + win_height = win.height(); + doc_height = doc.height(); if (offset_top == null) { offset_top = 0; } @@ -29,7 +33,6 @@ if (sticky_class == null) { sticky_class = "is_stuck"; } - doc = $(document); if (enable_bottoming == null) { enable_bottoming = true; } @@ -47,13 +50,14 @@ return el.outerWidth(true); } }; + ref = this; fn = function(elm, padding_bottom, parent_top, parent_height, top, height, el_float, detached) { var bottomed, detach, fixed, last_pos, last_scroll_height, offset, parent, recalc, recalc_and_tick, recalc_counter, spacer, tick; if (elm.data("sticky_kit")) { return; } elm.data("sticky_kit", true); - last_scroll_height = doc.height(); + last_scroll_height = doc_height; parent = elm.parent(); if (parent_selector != null) { parent = parent.closest(parent_selector); @@ -72,7 +76,9 @@ if (detached) { return; } - last_scroll_height = doc.height(); + win_height = win.height(); + doc_height = doc.height(); + last_scroll_height = doc_height; 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); @@ -93,7 +99,7 @@ }).removeClass(sticky_class); restore = true; } - top = elm.offset().top - (parseInt(elm.css("margin-top"), 10) || 0) - offset_top; + top = set_bottom ? 0 : elm.offset().top - (parseInt(elm.css("margin-top"), 10) || 0) - offset_top; height = elm.outerHeight(true); el_float = elm.css("float"); if (spacer) { @@ -115,9 +121,14 @@ } last_pos = void 0; offset = offset_top; + if (set_bottom) { + win.resize(function() { + return offset = offset_top = win.height() - bar_height; + }); + } recalc_counter = recalc_every; tick = function() { - var css, delta, recalced, scroll, will_bottom, win_height; + var css, delta, recalced, scroll, will_bottom; if (detached) { return; } @@ -168,7 +179,6 @@ 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; @@ -255,8 +265,8 @@ elm.on("sticky_kit:detach", detach); return setTimeout(tick, 0); }; - for (i = 0, len = this.length; i < len; i++) { - elm = this[i]; + for (i = 0, len = ref.length; i < len; i++) { + elm = ref[i]; fn($(elm)); } return this; diff --git a/spec/index.coffee b/spec/index.coffee index 7069b4c..2e7fb10 100644 --- a/spec/index.coffee +++ b/spec/index.coffee @@ -499,7 +499,48 @@ describe "sticky columns", -> expect(top cell).toBe 22 expect(cell.is(".is_stuck")).toBe false ] + + it "uses set_bottom", (done) -> + write_iframe(""" +
+
+
+
+ + + """).then (f) -> + cell = f.find(".stick_cell") + + # f.on "scroll", => console.warn f.scrollTop(), top cell + + scroll_each f, done, [ + at 5, => + expect(cell.is(".is_stuck")).toBe true + expect(cell.css("bottom")).toBe "0px" + + at 15, => + expect(cell.is(".is_stuck")).toBe true + expect(cell.css("bottom")).toBe "0px" + + at 40, => + expect(cell.is(".is_stuck")).toBe true + expect(cell.css("bottom")).toBe "0px" + at 125, => + expect(cell.is(".is_stuck")).toBe true + expect(cell.css("bottom")).toBe "0px" + + at 15, => + expect(cell.is(".is_stuck")).toBe true + expect(cell.css("bottom")).toBe "0px" + + at 0, => + expect(cell.is(".is_stuck")).toBe true + expect(cell.css("bottom")).toBe "0px" + ] describe "events", -> it "detects events when scrolling sticky header", (done) -> @@ -719,4 +760,3 @@ scroll_each = (f, done, points) -> done() scroll_to_next() - diff --git a/sticky-kit.coffee b/sticky-kit.coffee index 5eae00f..a9d1f2d 100644 --- a/sticky-kit.coffee +++ b/sticky-kit.coffee @@ -16,6 +16,8 @@ $.fn.stick_in_parent = (opts={}) -> offset_top spacer: manual_spacer bottoming: enable_bottoming + set_bottom + bar_height } = opts win_height = win.height() @@ -92,7 +94,7 @@ $.fn.stick_in_parent = (opts={}) -> restore = true - top = elm.offset().top - (parseInt(elm.css("margin-top"), 10) or 0) - offset_top + top = if set_bottom then 0 else elm.offset().top - (parseInt(elm.css("margin-top"), 10) or 0) - offset_top height = elm.outerHeight true @@ -114,6 +116,10 @@ $.fn.stick_in_parent = (opts={}) -> last_pos = undefined offset = offset_top + if set_bottom + win.resize -> + offset = offset_top = win.height() - bar_height + recalc_counter = recalc_every tick = -> @@ -127,7 +133,7 @@ $.fn.stick_in_parent = (opts={}) -> recalc() recalced = true - if !recalced && doc_height != last_scroll_height + if !recalced && doc.height() != last_scroll_height recalc() recalced = true @@ -265,5 +271,3 @@ $.fn.stick_in_parent = (opts={}) -> ) $ elm @ - -