scrollspy.js 4.53 KB
Newer Older
1
/* ========================================================================
fat's avatar
fat committed
2
 * Bootstrap: scrollspy.js v3.0.0
Chris Rebert's avatar
Chris Rebert committed
3
 * http://twbs.github.com/bootstrap/javascript.html#scrollspy
4
 * ========================================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 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

21
+function ($) { "use strict";
22

fat's avatar
fat committed
23
24
  // SCROLLSPY CLASS DEFINITION
  // ==========================
25

Jacob Thornton's avatar
Jacob Thornton committed
26
  function ScrollSpy(element, options) {
fat's avatar
fat committed
27
28
29
    var href
    var process  = $.proxy(this.process, this)

fat's avatar
fat committed
30
    this.$element       = $(element).is('body') ? $(window) : $(element)
fat's avatar
fat committed
31
    this.$body          = $('body')
fat's avatar
fat committed
32
    this.$scrollElement = this.$element.on('scroll.bs.scroll-spy.data-api', process)
fat's avatar
fat committed
33
34
    this.options        = $.extend({}, ScrollSpy.DEFAULTS, options)
    this.selector       = (this.options.target
Jacob Thornton's avatar
Jacob Thornton committed
35
      || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
36
      || '') + ' .nav li > a'
fat's avatar
fat committed
37
38
39
40
    this.offsets        = $([])
    this.targets        = $([])
    this.activeTarget   = null

41
    this.refresh()
42
    this.process()
Jacob Thornton's avatar
Jacob Thornton committed
43
44
  }

fat's avatar
fat committed
45
46
47
48
49
  ScrollSpy.DEFAULTS = {
    offset: 10
  }

  ScrollSpy.prototype.refresh = function () {
fat's avatar
fat committed
50
51
    var offsetMethod = this.$element[0] == window ? 'offset' : 'position'

fat's avatar
fat committed
52
53
54
55
56
57
58
59
60
61
62
63
64
    this.offsets = $([])
    this.targets = $([])

    var self     = this
    var $targets = this.$body
      .find(this.selector)
      .map(function () {
        var $el   = $(this)
        var href  = $el.data('target') || $el.attr('href')
        var $href = /^#\w/.test(href) && $(href)

        return ($href
          && $href.length
fat's avatar
fat committed
65
          && [[ $href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
fat's avatar
fat committed
66
67
68
69
70
71
72
      })
      .sort(function (a, b) { return a[0] - b[0] })
      .each(function () {
        self.offsets.push(this[0])
        self.targets.push(this[1])
      })
  }
Jacob Thornton's avatar
Jacob Thornton committed
73

fat's avatar
fat committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  ScrollSpy.prototype.process = function () {
    var scrollTop    = this.$scrollElement.scrollTop() + this.options.offset
    var scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
    var maxScroll    = scrollHeight - this.$scrollElement.height()
    var offsets      = this.offsets
    var targets      = this.targets
    var activeTarget = this.activeTarget
    var i

    if (scrollTop >= maxScroll) {
      return activeTarget != (i = targets.last()[0]) && this.activate(i)
    }

    for (i = offsets.length; i--;) {
      activeTarget != targets[i]
        && scrollTop >= offsets[i]
        && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
        && this.activate( targets[i] )
    }
Jacob Thornton's avatar
Jacob Thornton committed
93
94
  }

fat's avatar
fat committed
95
96
97
98
99
100
101
102
103
104
  ScrollSpy.prototype.activate = function (target) {
    this.activeTarget = target

    $(this.selector)
      .parents('.active')
      .removeClass('active')

    var selector = this.selector
      + '[data-target="' + target + '"],'
      + this.selector + '[href="' + target + '"]'
105

fat's avatar
fat committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    var active = $(selector)
      .parents('li')
      .addClass('active')

    if (active.parent('.dropdown-menu').length)  {
      active = active
        .closest('li.dropdown')
        .addClass('active')
    }

    active.trigger('activate')
  }


  // SCROLLSPY PLUGIN DEFINITION
  // ===========================
122

123
124
  var old = $.fn.scrollspy

Jacob Thornton's avatar
Jacob Thornton committed
125
  $.fn.scrollspy = function (option) {
126
    return this.each(function () {
fat's avatar
fat committed
127
      var $this   = $(this)
128
      var data    = $this.data('bs.scrollspy')
fat's avatar
fat committed
129
130
      var options = typeof option == 'object' && option

131
      if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
132
133
134
135
      if (typeof option == 'string') data[option]()
    })
  }

136
  $.fn.scrollspy.Constructor = ScrollSpy
137
138


fat's avatar
fat committed
139
140
  // SCROLLSPY NO CONFLICT
  // =====================
141
142
143
144
145
146
147

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


fat's avatar
fat committed
148
149
  // SCROLLSPY DATA-API
  // ==================
150

Jacob Thornton's avatar
Jacob Thornton committed
151
  $(window).on('load', function () {
152
153
154
155
    $('[data-spy="scroll"]').each(function () {
      var $spy = $(this)
      $spy.scrollspy($spy.data())
    })
156
  })
157

158
}(window.jQuery);