js/bootstrap-affix.js
author indvd00m (gotoindvdum[at]gmail[dot]com)
Fri, 04 Jul 2014 16:42:41 +0400
changeset 0 ba8ab09f730e
permissions -rw-r--r--
First home page
indvd00m@0
     1
/* ==========================================================
indvd00m@0
     2
 * bootstrap-affix.js v2.3.1
indvd00m@0
     3
 * http://twitter.github.com/bootstrap/javascript.html#affix
indvd00m@0
     4
 * ==========================================================
indvd00m@0
     5
 * Copyright 2012 Twitter, Inc.
indvd00m@0
     6
 *
indvd00m@0
     7
 * Licensed under the Apache License, Version 2.0 (the "License");
indvd00m@0
     8
 * you may not use this file except in compliance with the License.
indvd00m@0
     9
 * You may obtain a copy of the License at
indvd00m@0
    10
 *
indvd00m@0
    11
 * http://www.apache.org/licenses/LICENSE-2.0
indvd00m@0
    12
 *
indvd00m@0
    13
 * Unless required by applicable law or agreed to in writing, software
indvd00m@0
    14
 * distributed under the License is distributed on an "AS IS" BASIS,
indvd00m@0
    15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
indvd00m@0
    16
 * See the License for the specific language governing permissions and
indvd00m@0
    17
 * limitations under the License.
indvd00m@0
    18
 * ========================================================== */
indvd00m@0
    19
indvd00m@0
    20
indvd00m@0
    21
!function ($) {
indvd00m@0
    22
indvd00m@0
    23
  "use strict"; // jshint ;_;
indvd00m@0
    24
indvd00m@0
    25
indvd00m@0
    26
 /* AFFIX CLASS DEFINITION
indvd00m@0
    27
  * ====================== */
indvd00m@0
    28
indvd00m@0
    29
  var Affix = function (element, options) {
indvd00m@0
    30
    this.options = $.extend({}, $.fn.affix.defaults, options)
indvd00m@0
    31
    this.$window = $(window)
indvd00m@0
    32
      .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
indvd00m@0
    33
      .on('click.affix.data-api',  $.proxy(function () { setTimeout($.proxy(this.checkPosition, this), 1) }, this))
indvd00m@0
    34
    this.$element = $(element)
indvd00m@0
    35
    this.checkPosition()
indvd00m@0
    36
  }
indvd00m@0
    37
indvd00m@0
    38
  Affix.prototype.checkPosition = function () {
indvd00m@0
    39
    if (!this.$element.is(':visible')) return
indvd00m@0
    40
indvd00m@0
    41
    var scrollHeight = $(document).height()
indvd00m@0
    42
      , scrollTop = this.$window.scrollTop()
indvd00m@0
    43
      , position = this.$element.offset()
indvd00m@0
    44
      , offset = this.options.offset
indvd00m@0
    45
      , offsetBottom = offset.bottom
indvd00m@0
    46
      , offsetTop = offset.top
indvd00m@0
    47
      , reset = 'affix affix-top affix-bottom'
indvd00m@0
    48
      , affix
indvd00m@0
    49
indvd00m@0
    50
    if (typeof offset != 'object') offsetBottom = offsetTop = offset
indvd00m@0
    51
    if (typeof offsetTop == 'function') offsetTop = offset.top()
indvd00m@0
    52
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
indvd00m@0
    53
indvd00m@0
    54
    affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
indvd00m@0
    55
      false    : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
indvd00m@0
    56
      'bottom' : offsetTop != null && scrollTop <= offsetTop ?
indvd00m@0
    57
      'top'    : false
indvd00m@0
    58
indvd00m@0
    59
    if (this.affixed === affix) return
indvd00m@0
    60
indvd00m@0
    61
    this.affixed = affix
indvd00m@0
    62
    this.unpin = affix == 'bottom' ? position.top - scrollTop : null
indvd00m@0
    63
indvd00m@0
    64
    this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
indvd00m@0
    65
  }
indvd00m@0
    66
indvd00m@0
    67
indvd00m@0
    68
 /* AFFIX PLUGIN DEFINITION
indvd00m@0
    69
  * ======================= */
indvd00m@0
    70
indvd00m@0
    71
  var old = $.fn.affix
indvd00m@0
    72
indvd00m@0
    73
  $.fn.affix = function (option) {
indvd00m@0
    74
    return this.each(function () {
indvd00m@0
    75
      var $this = $(this)
indvd00m@0
    76
        , data = $this.data('affix')
indvd00m@0
    77
        , options = typeof option == 'object' && option
indvd00m@0
    78
      if (!data) $this.data('affix', (data = new Affix(this, options)))
indvd00m@0
    79
      if (typeof option == 'string') data[option]()
indvd00m@0
    80
    })
indvd00m@0
    81
  }
indvd00m@0
    82
indvd00m@0
    83
  $.fn.affix.Constructor = Affix
indvd00m@0
    84
indvd00m@0
    85
  $.fn.affix.defaults = {
indvd00m@0
    86
    offset: 0
indvd00m@0
    87
  }
indvd00m@0
    88
indvd00m@0
    89
indvd00m@0
    90
 /* AFFIX NO CONFLICT
indvd00m@0
    91
  * ================= */
indvd00m@0
    92
indvd00m@0
    93
  $.fn.affix.noConflict = function () {
indvd00m@0
    94
    $.fn.affix = old
indvd00m@0
    95
    return this
indvd00m@0
    96
  }
indvd00m@0
    97
indvd00m@0
    98
indvd00m@0
    99
 /* AFFIX DATA-API
indvd00m@0
   100
  * ============== */
indvd00m@0
   101
indvd00m@0
   102
  $(window).on('load', function () {
indvd00m@0
   103
    $('[data-spy="affix"]').each(function () {
indvd00m@0
   104
      var $spy = $(this)
indvd00m@0
   105
        , data = $spy.data()
indvd00m@0
   106
indvd00m@0
   107
      data.offset = data.offset || {}
indvd00m@0
   108
indvd00m@0
   109
      data.offsetBottom && (data.offset.bottom = data.offsetBottom)
indvd00m@0
   110
      data.offsetTop && (data.offset.top = data.offsetTop)
indvd00m@0
   111
indvd00m@0
   112
      $spy.affix(data)
indvd00m@0
   113
    })
indvd00m@0
   114
  })
indvd00m@0
   115
indvd00m@0
   116
indvd00m@0
   117
}(window.jQuery);