scrollspy.js 4.53 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: scrollspy.js v3.1.1
3
 * http://getbootstrap.com/javascript/#scrollspy
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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
+function ($) {
  'use strict';

  // SCROLLSPY CLASS DEFINITION
  // ==========================

  function ScrollSpy(element, options) {
    var process  = $.proxy(this.process, this)

    this.$body          = $('body')
    this.$scrollElement = $(element).is('body') ? $(window) : $(element)
    this.options        = $.extend({}, ScrollSpy.DEFAULTS, options)
    this.selector       = (this.options.target || '') + ' .nav li > a'
    this.offsets        = []
    this.targets        = []
    this.activeTarget   = null
    this.scrollHeight   = 0

    this.$scrollElement.on('scroll.bs.scrollspy', process)
    this.refresh()
    this.process()
  }

  ScrollSpy.VERSION  = '3.1.1'

  ScrollSpy.DEFAULTS = {
    offset: 10
  }

  ScrollSpy.prototype.getScrollHeight = function () {
    return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight)
  }

  ScrollSpy.prototype.refresh = function () {
    var offsetMethod = 'offset'
    var offsetBase   = 0

    if (!$.isWindow(this.$scrollElement[0])) {
      offsetMethod = 'position'
      offsetBase   = this.$scrollElement.scrollTop()
    }
51

52
53
54
    this.offsets = []
    this.targets = []
    this.scrollHeight = this.getScrollHeight()
XhmikosR's avatar
XhmikosR committed
55

56
    var self     = this
fat's avatar
fat committed
57

58
59
60
61
62
63
    this.$body
      .find(this.selector)
      .map(function () {
        var $el   = $(this)
        var href  = $el.data('target') || $el.attr('href')
        var $href = /^#./.test(href) && $(href)
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        return ($href
          && $href.length
          && $href.is(':visible')
          && [[$href[offsetMethod]().top + offsetBase, href]]) || null
      })
      .sort(function (a, b) { return a[0] - b[0] })
      .each(function () {
        self.offsets.push(this[0])
        self.targets.push(this[1])
      })
  }

  ScrollSpy.prototype.process = function () {
    var scrollTop    = this.$scrollElement.scrollTop() + this.options.offset
    var scrollHeight = this.getScrollHeight()
    var maxScroll    = this.options.offset + scrollHeight - this.$scrollElement.height()
    var offsets      = this.offsets
    var targets      = this.targets
    var activeTarget = this.activeTarget
    var i

    if (this.scrollHeight != scrollHeight) {
87
88
89
      this.refresh()
    }

90
91
    if (scrollTop >= maxScroll) {
      return activeTarget != (i = targets[targets.length - 1]) && this.activate(i)
fat's avatar
fat committed
92
93
    }

94
95
    if (activeTarget && scrollTop <= offsets[0]) {
      return activeTarget != (i = targets[0]) && this.activate(i)
96
97
    }

98
99
100
101
102
    for (i = offsets.length; i--;) {
      activeTarget != targets[i]
        && scrollTop >= offsets[i]
        && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
        && this.activate(targets[i])
fat's avatar
fat committed
103
    }
104
  }
Jacob Thornton's avatar
Jacob Thornton committed
105

106
107
  ScrollSpy.prototype.activate = function (target) {
    this.activeTarget = target
fat's avatar
fat committed
108

109
110
111
    $(this.selector)
      .parentsUntil(this.options.target, '.active')
      .removeClass('active')
fat's avatar
fat committed
112

113
114
115
    var selector = this.selector +
        '[data-target="' + target + '"],' +
        this.selector + '[href="' + target + '"]'
116

117
118
119
    var active = $(selector)
      .parents('li')
      .addClass('active')
fat's avatar
fat committed
120

121
122
123
    if (active.parent('.dropdown-menu').length) {
      active = active
        .closest('li.dropdown')
fat's avatar
fat committed
124
        .addClass('active')
125
    }
fat's avatar
fat committed
126

127
128
    active.trigger('activate.bs.scrollspy')
  }
129

fat's avatar
fat committed
130

131
132
  // SCROLLSPY PLUGIN DEFINITION
  // ===========================
133

134
135
136
137
138
  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.scrollspy')
      var options = typeof option == 'object' && option
139

140
141
142
143
      if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
144

145
  var old = $.fn.scrollspy
146

147
148
  $.fn.scrollspy             = Plugin
  $.fn.scrollspy.Constructor = ScrollSpy
149
150


151
152
  // SCROLLSPY NO CONFLICT
  // =====================
153

154
155
156
157
  $.fn.scrollspy.noConflict = function () {
    $.fn.scrollspy = old
    return this
  }
158

159

160
161
  // SCROLLSPY DATA-API
  // ==================
162

163
164
165
166
167
  $(window).on('load.bs.scrollspy.data-api', function () {
    $('[data-spy="scroll"]').each(function () {
      var $spy = $(this)
      Plugin.call($spy, $spy.data())
    })
168
  })
169

170
}(jQuery);