affix.js 3.77 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: affix.js v3.0.3
3
 * http://getbootstrap.com/javascript/#affix
4
 * ========================================================================
Zlatan Vasović's avatar
Zlatan Vasović committed
5
 * Copyright 2013 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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.
18
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
19
20


XhmikosR's avatar
XhmikosR committed
21
+function ($) { 'use strict';
Jacob Thornton's avatar
Jacob Thornton committed
22

fat's avatar
fat committed
23
24
  // AFFIX CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
25
26

  var Affix = function (element, options) {
fat's avatar
fat committed
27
    this.options = $.extend({}, Affix.DEFAULTS, options)
28
    this.$window = $(window)
29
30
      .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
      .on('click.bs.affix.data-api',  $.proxy(this.checkPositionWithEventLoop, this))
fat's avatar
fat committed
31

Jacob Thornton's avatar
Jacob Thornton committed
32
    this.$element = $(element)
fat's avatar
fat committed
33
34
35
    this.affixed  =
    this.unpin    = null

Jacob Thornton's avatar
stray ;    
Jacob Thornton committed
36
    this.checkPosition()
Jacob Thornton's avatar
Jacob Thornton committed
37
38
  }

fat's avatar
fat committed
39
40
  Affix.RESET = 'affix affix-top affix-bottom'

fat's avatar
fat committed
41
42
43
44
45
46
47
48
  Affix.DEFAULTS = {
    offset: 0
  }

  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }

Jacob Thornton's avatar
Jacob Thornton committed
49
50
51
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return

52
    var scrollHeight = $(document).height()
fat's avatar
fat committed
53
54
55
56
57
58
59
60
    var scrollTop    = this.$window.scrollTop()
    var position     = this.$element.offset()
    var offset       = this.options.offset
    var offsetTop    = offset.top
    var offsetBottom = offset.bottom

    if (typeof offset != 'object')         offsetBottom = offsetTop = offset
    if (typeof offsetTop == 'function')    offsetTop    = offset.top()
61
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
Jacob Thornton's avatar
Jacob Thornton committed
62

fat's avatar
fat committed
63
64
65
    var 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
66

67
    if (this.affixed === affix) return
fat's avatar
fat committed
68
    if (this.unpin) this.$element.css('top', '')
Jacob Thornton's avatar
Jacob Thornton committed
69
70

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

fat's avatar
fat committed
73
74
75
76
77
    this.$element.removeClass(Affix.RESET).addClass('affix' + (affix ? '-' + affix : ''))

    if (affix == 'bottom') {
      this.$element.offset({ top: document.body.offsetHeight - offsetBottom - this.$element.height() })
    }
Jacob Thornton's avatar
Jacob Thornton committed
78
79
80
  }


fat's avatar
fat committed
81
82
  // AFFIX PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
83

84
85
  var old = $.fn.affix

Jacob Thornton's avatar
Jacob Thornton committed
86
87
  $.fn.affix = function (option) {
    return this.each(function () {
fat's avatar
fat committed
88
      var $this   = $(this)
89
      var data    = $this.data('bs.affix')
fat's avatar
fat committed
90
91
      var options = typeof option == 'object' && option

92
      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
93
94
95
96
97
98
99
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.affix.Constructor = Affix


fat's avatar
fat committed
100
101
  // AFFIX NO CONFLICT
  // =================
102
103
104
105
106
107
108

  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }


fat's avatar
fat committed
109
110
  // AFFIX DATA-API
  // ==============
Jacob Thornton's avatar
Jacob Thornton committed
111

112
  $(window).on('load', function () {
Jacob Thornton's avatar
Jacob Thornton committed
113
114
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
fat's avatar
fat committed
115
      var data = $spy.data()
116

Jacob Thornton's avatar
Jacob Thornton committed
117
118
      data.offset = data.offset || {}

fat's avatar
fat committed
119
120
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
Jacob Thornton's avatar
Jacob Thornton committed
121
122
123
124
125

      $spy.affix(data)
    })
  })

126
}(jQuery);