affix.js 4.68 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: affix.js v3.2.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


10
11
+function ($) {
  'use strict';
Katie Zhu's avatar
Katie Zhu committed
12

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

16
17
  var Affix = function (element, options) {
    this.options = $.extend({}, Affix.DEFAULTS, options)
Jacob Thornton's avatar
Jacob Thornton committed
18

19
20
21
    this.$target = $(this.options.target)
      .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
      .on('click.bs.affix.data-api',  $.proxy(this.checkPositionWithEventLoop, this))
22

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

28
29
    this.checkPosition()
  }
fat's avatar
fat committed
30

Mark Otto's avatar
Mark Otto committed
31
  Affix.VERSION  = '3.2.0'
fat's avatar
fat committed
32

33
  Affix.RESET    = 'affix affix-top affix-bottom'
fat's avatar
fat committed
34

35
36
37
38
  Affix.DEFAULTS = {
    offset: 0,
    target: window
  }
fat's avatar
fat committed
39

40
  Affix.prototype.getState = function (scrollHeight, height, offsetTop, offsetBottom) {
fat's avatar
fat committed
41
42
43
44
45
    var scrollTop    = this.$target.scrollTop()
    var position     = this.$element.offset()
    var targetHeight = this.$target.height()

    if (offsetTop != null && this.affixed == 'top') return scrollTop < offsetTop ? 'top' : false
46
47

    if (this.affixed == 'bottom') {
fat's avatar
fat committed
48
49
      if (offsetTop != null) return (scrollTop + this.unpin <= position.top) ? false : 'bottom'
      return (scrollTop + targetHeight <= scrollHeight - offsetBottom) ? false : 'bottom'
50
    }
fat's avatar
fat committed
51
52
53
54
55
56
57
58
59

    var initializing   = this.affixed == null
    var colliderTop    = initializing ? scrollTop : position.top
    var colliderHeight = initializing ? targetHeight : height

    if (offsetTop != null && colliderTop <= offsetTop) return 'top'
    if (offsetBottom != null && (colliderTop + colliderHeight >= scrollHeight - offsetBottom)) return 'bottom'

    return false
60
61
  }

62
63
64
65
66
67
68
  Affix.prototype.getPinnedOffset = function () {
    if (this.pinnedOffset) return this.pinnedOffset
    this.$element.removeClass(Affix.RESET).addClass('affix')
    var scrollTop = this.$target.scrollTop()
    var position  = this.$element.offset()
    return (this.pinnedOffset = position.top - scrollTop)
  }
fat's avatar
fat committed
69

70
71
72
  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }
fat's avatar
fat committed
73

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

77
    var height       = this.$element.height()
78
79
80
    var offset       = this.options.offset
    var offsetTop    = offset.top
    var offsetBottom = offset.bottom
fat's avatar
fat committed
81
    var scrollHeight = $('body').height()
fat's avatar
fat committed
82

83
84
85
    if (typeof offset != 'object')         offsetBottom = offsetTop = offset
    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
86

87
    var affix = this.getState(scrollHeight, height, offsetTop, offsetBottom)
Jacob Thornton's avatar
Jacob Thornton committed
88

89
90
    if (this.affixed != affix) {
      if (this.unpin != null) this.$element.css('top', '')
Jacob Thornton's avatar
Jacob Thornton committed
91

92
93
      var affixType = 'affix' + (affix ? '-' + affix : '')
      var e         = $.Event(affixType + '.bs.affix')
94

95
      this.$element.trigger(e)
96

97
      if (e.isDefaultPrevented()) return
98

99
100
      this.affixed = affix
      this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
Jacob Thornton's avatar
Jacob Thornton committed
101

102
103
104
105
106
      this.$element
        .removeClass(Affix.RESET)
        .addClass(affixType)
        .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
    }
fat's avatar
fat committed
107

108
109
    if (affix == 'bottom') {
      this.$element.offset({
110
        top: scrollHeight - height - offsetBottom
111
      })
fat's avatar
fat committed
112
    }
113
  }
Jacob Thornton's avatar
Jacob Thornton committed
114
115


116
117
  // AFFIX PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
118

119
120
121
122
123
  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.affix')
      var options = typeof option == 'object' && option
fat's avatar
fat committed
124

125
126
127
128
      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
129

130
  var old = $.fn.affix
131

132
133
  $.fn.affix             = Plugin
  $.fn.affix.Constructor = Affix
Jacob Thornton's avatar
Jacob Thornton committed
134
135


136
137
  // AFFIX NO CONFLICT
  // =================
138

139
140
141
142
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
143
144


145
146
  // AFFIX DATA-API
  // ==============
Jacob Thornton's avatar
Jacob Thornton committed
147

148
149
150
151
  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
      var data = $spy.data()
152

153
      data.offset = data.offset || {}
Jacob Thornton's avatar
Jacob Thornton committed
154

155
156
      if (data.offsetBottom != null) data.offset.bottom = data.offsetBottom
      if (data.offsetTop    != null) data.offset.top    = data.offsetTop
Jacob Thornton's avatar
Jacob Thornton committed
157

158
      Plugin.call($spy, data)
Jacob Thornton's avatar
Jacob Thornton committed
159
160
161
    })
  })

162
}(jQuery);