docs/tutorial/jquery.sticky-kit.js
author Martin von Zweigbergk <martinvonz@google.com>
Thu, 19 Dec 2019 22:31:13 -0800
changeset 5019 967e9a87e82d
parent 3376 aad37ffd7d58
permissions -rw-r--r--
cleanup: replace .parents()[0] by .p1()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3376
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     1
// Generated by CoffeeScript 1.9.2
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     2
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     3
/**
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     4
@license Sticky-kit v1.1.2 | WTFPL | Leaf Corcoran 2015 | http://leafo.net
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     5
 */
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     6
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     7
(function() {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     8
  var $, win;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
     9
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    10
  $ = this.jQuery || window.jQuery;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    11
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    12
  win = $(window);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    13
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    14
  $.fn.stick_in_parent = function(opts) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    15
    var doc, elm, enable_bottoming, fn, i, inner_scrolling, len, manual_spacer, offset_top, parent_selector, recalc_every, sticky_class;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    16
    if (opts == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    17
      opts = {};
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    18
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    19
    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;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    20
    if (offset_top == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    21
      offset_top = 0;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    22
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    23
    if (parent_selector == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    24
      parent_selector = void 0;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    25
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    26
    if (inner_scrolling == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    27
      inner_scrolling = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    28
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    29
    if (sticky_class == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    30
      sticky_class = "is_stuck";
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    31
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    32
    doc = $(document);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    33
    if (enable_bottoming == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    34
      enable_bottoming = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    35
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    36
    fn = function(elm, padding_bottom, parent_top, parent_height, top, height, el_float, detached) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    37
      var bottomed, detach, fixed, last_pos, last_scroll_height, offset, parent, recalc, recalc_and_tick, recalc_counter, spacer, tick;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    38
      if (elm.data("sticky_kit")) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    39
        return;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    40
      }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    41
      elm.data("sticky_kit", true);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    42
      last_scroll_height = doc.height();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    43
      parent = elm.parent();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    44
      if (parent_selector != null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    45
        parent = parent.closest(parent_selector);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    46
      }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    47
      if (!parent.length) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    48
        throw "failed to find stick parent";
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    49
      }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    50
      fixed = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    51
      bottomed = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    52
      spacer = manual_spacer != null ? manual_spacer && elm.closest(manual_spacer) : $("<div />");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    53
      if (spacer) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    54
        spacer.css('position', elm.css('position'));
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    55
      }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    56
      recalc = function() {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    57
        var border_top, padding_top, restore;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    58
        if (detached) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    59
          return;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    60
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    61
        last_scroll_height = doc.height();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    62
        border_top = parseInt(parent.css("border-top-width"), 10);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    63
        padding_top = parseInt(parent.css("padding-top"), 10);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    64
        padding_bottom = parseInt(parent.css("padding-bottom"), 10);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    65
        parent_top = parent.offset().top + border_top + padding_top;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    66
        parent_height = parent.height();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    67
        if (fixed) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    68
          fixed = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    69
          bottomed = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    70
          if (manual_spacer == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    71
            elm.insertAfter(spacer);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    72
            spacer.detach();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    73
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    74
          elm.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    75
            position: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    76
            top: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    77
            width: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    78
            bottom: ""
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    79
          }).removeClass(sticky_class);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    80
          restore = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    81
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    82
        top = elm.offset().top - (parseInt(elm.css("margin-top"), 10) || 0) - offset_top;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    83
        height = elm.outerHeight(true);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    84
        el_float = elm.css("float");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    85
        if (spacer) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    86
          spacer.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    87
            width: elm.outerWidth(true),
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    88
            height: height,
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    89
            display: elm.css("display"),
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    90
            "vertical-align": elm.css("vertical-align"),
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    91
            "float": el_float
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    92
          });
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    93
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    94
        if (restore) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    95
          return tick();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    96
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    97
      };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    98
      recalc();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
    99
      if (height === parent_height) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   100
        return;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   101
      }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   102
      last_pos = void 0;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   103
      offset = offset_top;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   104
      recalc_counter = recalc_every;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   105
      tick = function() {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   106
        var css, delta, recalced, scroll, will_bottom, win_height;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   107
        if (detached) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   108
          return;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   109
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   110
        recalced = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   111
        if (recalc_counter != null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   112
          recalc_counter -= 1;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   113
          if (recalc_counter <= 0) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   114
            recalc_counter = recalc_every;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   115
            recalc();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   116
            recalced = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   117
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   118
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   119
        if (!recalced && doc.height() !== last_scroll_height) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   120
          recalc();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   121
          recalced = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   122
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   123
        scroll = win.scrollTop();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   124
        if (last_pos != null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   125
          delta = scroll - last_pos;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   126
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   127
        last_pos = scroll;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   128
        if (fixed) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   129
          if (enable_bottoming) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   130
            will_bottom = scroll + height + offset > parent_height + parent_top;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   131
            if (bottomed && !will_bottom) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   132
              bottomed = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   133
              elm.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   134
                position: "fixed",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   135
                bottom: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   136
                top: offset
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   137
              }).trigger("sticky_kit:unbottom");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   138
            }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   139
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   140
          if (scroll < top) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   141
            fixed = false;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   142
            offset = offset_top;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   143
            if (manual_spacer == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   144
              if (el_float === "left" || el_float === "right") {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   145
                elm.insertAfter(spacer);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   146
              }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   147
              spacer.detach();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   148
            }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   149
            css = {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   150
              position: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   151
              width: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   152
              top: ""
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   153
            };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   154
            elm.css(css).removeClass(sticky_class).trigger("sticky_kit:unstick");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   155
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   156
          if (inner_scrolling) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   157
            win_height = win.height();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   158
            if (height + offset_top > win_height) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   159
              if (!bottomed) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   160
                offset -= delta;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   161
                offset = Math.max(win_height - height, offset);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   162
                offset = Math.min(offset_top, offset);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   163
                if (fixed) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   164
                  elm.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   165
                    top: offset + "px"
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   166
                  });
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   167
                }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   168
              }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   169
            }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   170
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   171
        } else {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   172
          if (scroll > top) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   173
            fixed = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   174
            css = {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   175
              position: "fixed",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   176
              top: offset
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   177
            };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   178
            css.width = elm.css("box-sizing") === "border-box" ? elm.outerWidth() + "px" : elm.width() + "px";
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   179
            elm.css(css).addClass(sticky_class);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   180
            if (manual_spacer == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   181
              elm.after(spacer);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   182
              if (el_float === "left" || el_float === "right") {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   183
                spacer.append(elm);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   184
              }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   185
            }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   186
            elm.trigger("sticky_kit:stick");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   187
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   188
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   189
        if (fixed && enable_bottoming) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   190
          if (will_bottom == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   191
            will_bottom = scroll + height + offset > parent_height + parent_top;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   192
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   193
          if (!bottomed && will_bottom) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   194
            bottomed = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   195
            if (parent.css("position") === "static") {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   196
              parent.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   197
                position: "relative"
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   198
              });
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   199
            }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   200
            return elm.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   201
              position: "absolute",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   202
              bottom: padding_bottom,
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   203
              top: "auto"
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   204
            }).trigger("sticky_kit:bottom");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   205
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   206
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   207
      };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   208
      recalc_and_tick = function() {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   209
        recalc();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   210
        return tick();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   211
      };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   212
      detach = function() {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   213
        detached = true;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   214
        win.off("touchmove", tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   215
        win.off("scroll", tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   216
        win.off("resize", recalc_and_tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   217
        $(document.body).off("sticky_kit:recalc", recalc_and_tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   218
        elm.off("sticky_kit:detach", detach);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   219
        elm.removeData("sticky_kit");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   220
        elm.css({
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   221
          position: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   222
          bottom: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   223
          top: "",
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   224
          width: ""
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   225
        });
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   226
        parent.position("position", "");
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   227
        if (fixed) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   228
          if (manual_spacer == null) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   229
            if (el_float === "left" || el_float === "right") {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   230
              elm.insertAfter(spacer);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   231
            }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   232
            spacer.remove();
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   233
          }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   234
          return elm.removeClass(sticky_class);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   235
        }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   236
      };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   237
      win.on("touchmove", tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   238
      win.on("scroll", tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   239
      win.on("resize", recalc_and_tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   240
      $(document.body).on("sticky_kit:recalc", recalc_and_tick);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   241
      elm.on("sticky_kit:detach", detach);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   242
      return setTimeout(tick, 0);
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   243
    };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   244
    for (i = 0, len = this.length; i < len; i++) {
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   245
      elm = this[i];
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   246
      fn($(elm));
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   247
    }
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   248
    return this;
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   249
  };
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   250
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
   251
}).call(this);