scrollspy.js 4.41 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* =============================================================
fat's avatar
fat committed
2
 * Bootstrap: scrollspy.js v3.0.0
Jacob Thornton's avatar
Jacob Thornton committed
3
4
 * http://twitter.github.com/bootstrap/javascript.html#scrollspy
 * =============================================================
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.
Jacob Thornton's avatar
Jacob Thornton committed
18
19
 * ============================================================== */

20

fat's avatar
fat committed
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
30
31
    var href
    var process  = $.proxy(this.process, this)
    var $element = $(element).is('body') ? $(window) : $(element)

    this.$body          = $('body')
Jacob Thornton's avatar
Jacob Thornton committed
32
    this.$scrollElement = $element.on('scroll.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  ScrollSpy.DEFAULTS = {
    offset: 10
  }

  ScrollSpy.prototype.refresh = function () {
    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
          && [[ $href.position().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
      })
      .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
71

fat's avatar
fat committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  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
91
92
  }

fat's avatar
fat committed
93
94
95
96
97
98
99
100
101
102
  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 + '"]'
103

fat's avatar
fat committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    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
  // ===========================
120

121
122
  var old = $.fn.scrollspy

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

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

134
  $.fn.scrollspy.Constructor = ScrollSpy
135
136


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

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


fat's avatar
fat committed
146
147
  // SCROLLSPY DATA-API
  // ==================
148

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

156
}(window.jQuery);