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