scrollspy.js 4.33 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
37
38
39
  ScrollSpy.DEFAULTS = {
    offset: 10
  }

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

fat's avatar
fat committed
42
43
44
45
    this.offsets = $([])
    this.targets = $([])

    var self     = this
XhmikosR's avatar
XhmikosR committed
46
47

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

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

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

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

fat's avatar
fat committed
83
84
85
86
87
88
    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
89
90
  }

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

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

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

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

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

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


  // SCROLLSPY PLUGIN DEFINITION
  // ===========================
118

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

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

130
131
132
  var old = $.fn.scrollspy

  $.fn.scrollspy             = Plugin
133
  $.fn.scrollspy.Constructor = ScrollSpy
134
135


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

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


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

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

155
}(jQuery);