// Generated by CoffeeScript 1.9.1 /** @license Relatively-Sticky v2.0.0 | WTFPL | Tobias Bengfort 2015 */ (function() { var $, top, win; $ = this.jQuery || window.jQuery; win = $(window); top = function(element, context) { if (context === document) { if (element === document) { return 0; } else if (element === win) { return win.scrollTop(); } else { return element.offset().top; } } else { return top(element, document) - top(context, document); } }; $.fn.stick_in_parent = function(opts) { var elm, enable_bottoming, fn, i, len, offset_top, parent_selector, scrollable_selector, sticky_class; if (opts == null) { opts = {}; } sticky_class = opts.sticky_class, parent_selector = opts.parent, scrollable_selector = opts.scrollable, offset_top = opts.offset_top, enable_bottoming = opts.bottoming; if (offset_top == null) { offset_top = 0; } if (parent_selector == null) { parent_selector = void 0; } if (sticky_class == null) { sticky_class = "is_stuck"; } if (enable_bottoming == null) { enable_bottoming = true; } fn = function(elm, detached, fixed, bottomed) { var _top, detach, get_max, parent, scrollable, tick; if (elm.data("sticky_kit")) { return; } elm.data("sticky_kit", true); scrollable = win; if (scrollable_selector != null) { scrollable = elm.parent().closest(scrollable_selector); } if (!scrollable.length) { throw "failed to find stick scrollable"; } parent = elm.parent(); if (parent_selector != null) { parent = parent.closest(parent_selector); } if (!parent.length) { throw "failed to find stick parent"; } _top = 0; elm.css("position", "relative"); get_max = function() { var border_bottom, margin_top, padding_bottom; padding_bottom = parseInt(parent.css("padding-bottom"), 10); border_bottom = parseInt(parent.css("border-bottom-width"), 10); margin_top = parseInt(elm.css("margin-top"), 10); return parent.outerHeight() - (elm.outerHeight(true) - margin_top) - padding_bottom - border_bottom; }; tick = function() { var bottomed_old, fixed_old, margin_top, max, original_top; if (detached) { return; } fixed_old = fixed; bottomed_old = bottomed; margin_top = parseInt(elm.css("margin-top"), 10); original_top = top(elm, parent) - _top; _top = top(scrollable, parent) + offset_top + margin_top; if (enable_bottoming) { max = get_max(); if (bottomed = _top >= max) { _top = max; } } _top = Math.max(_top - original_top, 0); elm.css("top", _top); if ((fixed = _top !== 0)) { elm.addClass(sticky_class); } else { elm.removeClass(sticky_class); } if (!fixed && fixed_old) { elm.trigger("sticky_kit:unstick"); } if (!bottomed && bottomed_old) { elm.trigger("sticky_kit:unbottom"); } if (fixed && !fixed_old) { return elm.trigger("sticky_kit:stick"); } else if (bottomed && !bottomed_old) { return elm.trigger("sticky_kit:bottom"); } }; detach = function() { detached = true; win.off("touchmove", tick); win.off("scroll", tick); win.off("resize", tick); if (scrollable_selector) { scrollable.off("touchmove", tick); scrollable.off("scroll", tick); elm.parentsUntil(scrollable).off("touchmove", tick); elm.parentsUntil(scrollable).off("scroll", tick); } $(document.body).off("sticky_kit:recalc", tick); elm.off("sticky_kit:detach", detach); elm.removeData("sticky_kit"); elm.css({ position: "", top: "" }); if (fixed) { return elm.removeClass(sticky_class); } }; win.on("touchmove", tick); win.on("scroll", tick); win.on("resize", tick); if (scrollable_selector) { scrollable.on("touchmove", tick); scrollable.on("scroll", tick); elm.parentsUntil(scrollable).on("touchmove", tick); elm.parentsUntil(scrollable).on("scroll", tick); } $(document.body).on("sticky_kit:recalc", 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);