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


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
19

    this.$target = $(this.options.target)
20
21
      .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
22

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

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

fat's avatar
fat committed
31
32
33
  Affix.VERSION  = '3.1.1'

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

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

fat's avatar
fat committed
40
41
42
  Affix.prototype.getPinnedOffset = function () {
    if (this.pinnedOffset) return this.pinnedOffset
    this.$element.removeClass(Affix.RESET).addClass('affix')
43
    var scrollTop = this.$target.scrollTop()
fat's avatar
fat committed
44
45
46
47
    var position  = this.$element.offset()
    return (this.pinnedOffset = position.top - scrollTop)
  }

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

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

55
    var scrollHeight = $(document).height()
56
    var scrollTop    = this.$target.scrollTop()
fat's avatar
fat committed
57
58
59
60
61
62
    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
63
64
    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

fat's avatar
fat committed
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
    if (this.affixed === affix) return
fat's avatar
fat committed
71
    if (this.unpin != null) this.$element.css('top', '')
Jacob Thornton's avatar
Jacob Thornton committed
72

73
74
75
76
77
78
79
    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
80
    this.affixed = affix
fat's avatar
fat committed
81
    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

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


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

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

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

110
111
112
  var old = $.fn.affix

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


fat's avatar
fat committed
116
117
  // AFFIX NO CONFLICT
  // =================
118
119
120
121
122
123
124

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


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

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

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

fat's avatar
fat committed
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);