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

31
  Affix.VERSION  = '3.1.1'
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
41
42
43
44
45
46
  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
47

48
49
50
  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }
fat's avatar
fat committed
51

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

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

62
63
64
    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
65

66
67
68
    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
69

70
71
    if (this.affixed === affix) return
    if (this.unpin != null) this.$element.css('top', '')
Jacob Thornton's avatar
Jacob Thornton committed
72

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

76
    this.$element.trigger(e)
77

78
    if (e.isDefaultPrevented()) return
79

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

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

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


96
97
  // AFFIX PLUGIN DEFINITION
  // =======================
Jacob Thornton's avatar
Jacob Thornton committed
98

99
100
101
102
103
  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
104

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

110
  var old = $.fn.affix
111

112
113
  $.fn.affix             = Plugin
  $.fn.affix.Constructor = Affix
Jacob Thornton's avatar
Jacob Thornton committed
114
115


116
117
  // AFFIX NO CONFLICT
  // =================
118

119
120
121
122
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
123
124


125
126
  // AFFIX DATA-API
  // ==============
Jacob Thornton's avatar
Jacob Thornton committed
127

128
129
130
131
  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
      var data = $spy.data()
132

133
      data.offset = data.offset || {}
Jacob Thornton's avatar
Jacob Thornton committed
134

135
136
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
Jacob Thornton's avatar
Jacob Thornton committed
137

138
      Plugin.call($spy, data)
Jacob Thornton's avatar
Jacob Thornton committed
139
140
141
    })
  })

142
}(jQuery);