scrollspy.js 4.56 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: scrollspy.js v3.2.0
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
+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()
  }

Mark Otto's avatar
Mark Otto committed
33
  ScrollSpy.VERSION  = '3.2.0'
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

  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
        return ($href
          && $href.length
          && $href.is(':visible')
68
          && $el.is(':visible')
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
          && [[$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) {
88
89
90
      this.refresh()
    }

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

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

99
100
101
102
103
    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
104
    }
105
  }
Jacob Thornton's avatar
Jacob Thornton committed
106

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

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

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

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

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

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

fat's avatar
fat committed
131

132
133
  // SCROLLSPY PLUGIN DEFINITION
  // ===========================
134

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

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

146
  var old = $.fn.scrollspy
147

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


152
153
  // SCROLLSPY NO CONFLICT
  // =====================
154

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

160

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

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

171
}(jQuery);