affix.js 3.94 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: affix.js v3.1.0
3
 * http://getbootstrap.com/javascript/#affix
4
 * ========================================================================
5
 * Copyright 2011-2014 Twitter, Inc.
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
8
9


Zlatan Vasović's avatar
Zlatan Vasović committed
10
11
+function ($) {
  'use strict';
Jacob Thornton's avatar
Jacob Thornton committed
12

fat's avatar
fat committed
13
14
  // AFFIX CLASS DEFINITION
  // ======================
Jacob Thornton's avatar
Jacob Thornton committed
15
16

  var Affix = function (element, options) {
fat's avatar
fat committed
17
    this.options = $.extend({}, Affix.DEFAULTS, options)
18
    this.$window = $(window)
19
20
      .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
21

fat's avatar
fat committed
22
23
24
25
    this.$element     = $(element)
    this.affixed      =
    this.unpin        =
    this.pinnedOffset = null
fat's avatar
fat committed
26

Jacob Thornton's avatar
stray ;    
Jacob Thornton committed
27
    this.checkPosition()
Jacob Thornton's avatar
Jacob Thornton committed
28
29
  }

fat's avatar
fat committed
30
31
  Affix.RESET = 'affix affix-top affix-bottom'

fat's avatar
fat committed
32
33
34
35
  Affix.DEFAULTS = {
    offset: 0
  }

fat's avatar
fat committed
36
37
38
39
40
41
42
43
  Affix.prototype.getPinnedOffset = function () {
    if (this.pinnedOffset) return this.pinnedOffset
    this.$element.removeClass(Affix.RESET).addClass('affix')
    var scrollTop = this.$window.scrollTop()
    var position  = this.$element.offset()
    return (this.pinnedOffset = position.top - scrollTop)
  }

fat's avatar
fat committed
44
45
46
47
  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }

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

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

fat's avatar
fat committed
58
59
    if (this.affixed == 'top') position.top += scrollTop

fat's avatar
fat committed
60
    if (typeof offset != 'object')         offsetBottom = offsetTop = offset
61
62
    if (typeof offsetTop == 'function')    offsetTop    = offset.top(this.$element)
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
Jacob Thornton's avatar
Jacob Thornton committed
63

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

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

71
72
73
74
75
76
77
    var affixType = 'affix' + (affix ? '-' + affix : '')
    var e         = $.Event(affixType + '.bs.affix')

    this.$element.trigger(e)

    if (e.isDefaultPrevented()) return

Jacob Thornton's avatar
Jacob Thornton committed
78
    this.affixed = affix
fat's avatar
fat committed
79
    this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
Jacob Thornton's avatar
Jacob Thornton committed
80

81
82
83
84
    this.$element
      .removeClass(Affix.RESET)
      .addClass(affixType)
      .trigger($.Event(affixType.replace('affix', 'affixed')))
fat's avatar
fat committed
85
86

    if (affix == 'bottom') {
87
      this.$element.offset({ top: scrollHeight - offsetBottom - this.$element.height() })
fat's avatar
fat committed
88
    }
Jacob Thornton's avatar
Jacob Thornton committed
89
90
91
  }


fat's avatar
fat committed
92
93
  // AFFIX PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
94

95
96
  var old = $.fn.affix

Jacob Thornton's avatar
Jacob Thornton committed
97
98
  $.fn.affix = function (option) {
    return this.each(function () {
fat's avatar
fat committed
99
      var $this   = $(this)
100
      var data    = $this.data('bs.affix')
fat's avatar
fat committed
101
102
      var options = typeof option == 'object' && option

103
      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
Jacob Thornton's avatar
Jacob Thornton committed
104
105
106
107
108
109
110
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.affix.Constructor = Affix


fat's avatar
fat committed
111
112
  // AFFIX NO CONFLICT
  // =================
113
114
115
116
117
118
119

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


fat's avatar
fat committed
120
121
  // AFFIX DATA-API
  // ==============
Jacob Thornton's avatar
Jacob Thornton committed
122

123
  $(window).on('load', function () {
Jacob Thornton's avatar
Jacob Thornton committed
124
125
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
fat's avatar
fat committed
126
      var data = $spy.data()
127

Jacob Thornton's avatar
Jacob Thornton committed
128
129
      data.offset = data.offset || {}

fat's avatar
fat committed
130
131
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
Jacob Thornton's avatar
Jacob Thornton committed
132
133
134
135
136

      $spy.affix(data)
    })
  })

137
}(jQuery);