bootstrap-affix.js 3.25 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-affix.js v2.1.2
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * http://twitter.github.com/bootstrap/javascript.html#affix
 * ==========================================================
 * Copyright 2012 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */


!function ($) {

  "use strict"; // jshint ;_;


 /* AFFIX CLASS DEFINITION
  * ====================== */

  var Affix = function (element, options) {
    this.options = $.extend({}, $.fn.affix.defaults, options)
31
32
33
    this.$window = $(window)
      .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
      .on('click.affix.data-api',  $.proxy(function () { setTimeout($.proxy(this.checkPosition, this), 1) }, this))
Jacob Thornton's avatar
Jacob Thornton committed
34
    this.$element = $(element)
Jacob Thornton's avatar
stray ;    
Jacob Thornton committed
35
    this.checkPosition()
Jacob Thornton's avatar
Jacob Thornton committed
36
37
38
39
40
  }

  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return

41
    var scrollHeight = $(document).height()
Jacob Thornton's avatar
Jacob Thornton committed
42
      , scrollTop = this.$window.scrollTop()
43
      , position = this.$element.offset()
Jacob Thornton's avatar
Jacob Thornton committed
44
      , offset = this.options.offset
45
46
47
      , offsetBottom = offset.bottom
      , offsetTop = offset.top
      , reset = 'affix affix-top affix-bottom'
Jacob Thornton's avatar
Jacob Thornton committed
48
49
      , affix

50
51
52
    if (typeof offset != 'object') offsetBottom = offsetTop = offset
    if (typeof offsetTop == 'function') offsetTop = offset.top()
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
Jacob Thornton's avatar
Jacob Thornton committed
53

54
55
56
57
    affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
      false    : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
      'bottom' : offsetTop != null && scrollTop <= offsetTop ?
      'top'    : false
Jacob Thornton's avatar
Jacob Thornton committed
58

59
    if (this.affixed === affix) return
Jacob Thornton's avatar
Jacob Thornton committed
60
61

    this.affixed = affix
62
    this.unpin = affix == 'bottom' ? position.top - scrollTop : null
Jacob Thornton's avatar
Jacob Thornton committed
63

64
    this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
Jacob Thornton's avatar
Jacob Thornton committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  }


 /* AFFIX PLUGIN DEFINITION
  * ======================= */

  $.fn.affix = function (option) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('affix')
        , options = typeof option == 'object' && option
      if (!data) $this.data('affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.affix.Constructor = Affix

  $.fn.affix.defaults = {
    offset: 0
  }


 /* AFFIX DATA-API
  * ============== */

91
  $(window).on('load', function () {
Jacob Thornton's avatar
Jacob Thornton committed
92
93
94
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
        , data = $spy.data()
95

Jacob Thornton's avatar
Jacob Thornton committed
96
97
      data.offset = data.offset || {}

98
99
      data.offsetBottom && (data.offset.bottom = data.offsetBottom)
      data.offsetTop && (data.offset.top = data.offsetTop)
Jacob Thornton's avatar
Jacob Thornton committed
100
101
102
103
104
105
106

      $spy.affix(data)
    })
  })


}(window.jQuery);