scrollspy.js 4.4 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

Zlatan Vasović's avatar
Zlatan Vasović committed
10
11
+function ($) {
  'use strict';
12

fat's avatar
fat committed
13
14
  // SCROLLSPY CLASS DEFINITION
  // ==========================
15

Jacob Thornton's avatar
Jacob Thornton committed
16
  function ScrollSpy(element, options) {
fat's avatar
fat committed
17
18
19
    var href
    var process  = $.proxy(this.process, this)

fat's avatar
fat committed
20
    this.$element       = $(element).is('body') ? $(window) : $(element)
fat's avatar
fat committed
21
    this.$body          = $('body')
22
    this.$scrollElement = this.$element.on('scroll.bs.scrollspy', process)
fat's avatar
fat committed
23
24
    this.options        = $.extend({}, ScrollSpy.DEFAULTS, options)
    this.selector       = (this.options.target
Jacob Thornton's avatar
Jacob Thornton committed
25
      || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
26
      || '') + ' .nav li > a'
fat's avatar
fat committed
27
28
29
30
    this.offsets        = $([])
    this.targets        = $([])
    this.activeTarget   = null

31
    this.refresh()
32
    this.process()
Jacob Thornton's avatar
Jacob Thornton committed
33
34
  }

fat's avatar
fat committed
35
36
  ScrollSpy.VERSION  = '3.1.1'

fat's avatar
fat committed
37
38
39
40
41
  ScrollSpy.DEFAULTS = {
    offset: 10
  }

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

fat's avatar
fat committed
44
45
46
    this.offsets = $([])
    this.targets = $([])
    var self     = this
XhmikosR's avatar
XhmikosR committed
47
48

    this.$body
fat's avatar
fat committed
49
      .find(this.selector)
50
      .filter(':visible')
fat's avatar
fat committed
51
52
53
      .map(function () {
        var $el   = $(this)
        var href  = $el.data('target') || $el.attr('href')
54
        var $href = /^#./.test(href) && $(href)
fat's avatar
fat committed
55
56
57

        return ($href
          && $href.length
58
          && $href.is(':visible')
XhmikosR's avatar
XhmikosR committed
59
          && [[$href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href]]) || null
fat's avatar
fat committed
60
61
62
63
64
65
66
      })
      .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
67

fat's avatar
fat committed
68
69
  ScrollSpy.prototype.process = function () {
    var scrollTop    = this.$scrollElement.scrollTop() + this.options.offset
70
    var scrollHeight = this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight)
71
    var maxScroll    = this.options.offset + scrollHeight - this.$scrollElement.height()
fat's avatar
fat committed
72
73
74
75
76
77
78
79
80
    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)
    }

81
    if (activeTarget && scrollTop <= offsets[0]) {
82
      return activeTarget != (i = targets[0]) && this.activate(i)
83
84
    }

fat's avatar
fat committed
85
86
87
88
    for (i = offsets.length; i--;) {
      activeTarget != targets[i]
        && scrollTop >= offsets[i]
        && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
XhmikosR's avatar
XhmikosR committed
89
        && this.activate(targets[i])
fat's avatar
fat committed
90
    }
Jacob Thornton's avatar
Jacob Thornton committed
91
92
  }

fat's avatar
fat committed
93
94
95
96
  ScrollSpy.prototype.activate = function (target) {
    this.activeTarget = target

    $(this.selector)
fat's avatar
fat committed
97
      .parentsUntil(this.options.target, '.active')
fat's avatar
fat committed
98
99
      .removeClass('active')

Zlatan Vasović's avatar
Zlatan Vasović committed
100
101
102
    var selector = this.selector +
        '[data-target="' + target + '"],' +
        this.selector + '[href="' + target + '"]'
103

fat's avatar
fat committed
104
105
106
107
    var active = $(selector)
      .parents('li')
      .addClass('active')

Zlatan Vasović's avatar
Zlatan Vasović committed
108
    if (active.parent('.dropdown-menu').length) {
fat's avatar
fat committed
109
110
111
112
113
      active = active
        .closest('li.dropdown')
        .addClass('active')
    }

114
    active.trigger('activate.bs.scrollspy')
fat's avatar
fat committed
115
116
117
118
119
  }


  // SCROLLSPY PLUGIN DEFINITION
  // ===========================
120

121
  function Plugin(option) {
122
    return this.each(function () {
fat's avatar
fat committed
123
      var $this   = $(this)
124
      var data    = $this.data('bs.scrollspy')
fat's avatar
fat committed
125
126
      var options = typeof option == 'object' && option

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

132
133
134
  var old = $.fn.scrollspy

  $.fn.scrollspy             = Plugin
135
  $.fn.scrollspy.Constructor = ScrollSpy
136
137


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

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


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

150
  $(window).on('load.bs.scrollspy.data-api', function () {
151
152
    $('[data-spy="scroll"]').each(function () {
      var $spy = $(this)
153
      Plugin.call($spy, $spy.data())
154
    })
155
  })
156

157
}(jQuery);