1 /* ========================================================================
2 * Bootstrap: scrollspy.js v3.0.3
3 * http://getbootstrap.com/javascript/#scrollspy
4 * ========================================================================
5 * Copyright 2013 Twitter, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ======================================================================== */
21 +function ($) { "use strict";
23 // SCROLLSPY CLASS DEFINITION
24 // ==========================
26 function ScrollSpy(element, options) {
28 var process = $.proxy(this.process, this)
30 this.$element = $(element).is('body') ? $(window) : $(element)
31 this.$body = $('body')
32 this.$scrollElement = this.$element.on('scroll.bs.scroll-spy.data-api', process)
33 this.options = $.extend({}, ScrollSpy.DEFAULTS, options)
34 this.selector = (this.options.target
35 || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
36 || '') + ' .nav li > a'
39 this.activeTarget = null
45 ScrollSpy.DEFAULTS = {
49 ScrollSpy.prototype.refresh = function () {
50 var offsetMethod = this.$element[0] == window ? 'offset' : 'position'
56 var $targets = this.$body
60 var href = $el.data('target') || $el.attr('href')
61 var $href = /^#\w/.test(href) && $(href)
65 && [[ $href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
67 .sort(function (a, b) { return a[0] - b[0] })
69 self.offsets.push(this[0])
70 self.targets.push(this[1])
74 ScrollSpy.prototype.process = function () {
75 var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
76 var scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
77 var maxScroll = scrollHeight - this.$scrollElement.height()
78 var offsets = this.offsets
79 var targets = this.targets
80 var activeTarget = this.activeTarget
83 if (scrollTop >= maxScroll) {
84 return activeTarget != (i = targets.last()[0]) && this.activate(i)
87 for (i = offsets.length; i--;) {
88 activeTarget != targets[i]
89 && scrollTop >= offsets[i]
90 && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
91 && this.activate( targets[i] )
95 ScrollSpy.prototype.activate = function (target) {
96 this.activeTarget = target
100 .removeClass('active')
102 var selector = this.selector
103 + '[data-target="' + target + '"],'
104 + this.selector + '[href="' + target + '"]'
106 var active = $(selector)
110 if (active.parent('.dropdown-menu').length) {
112 .closest('li.dropdown')
116 active.trigger('activate.bs.scrollspy')
120 // SCROLLSPY PLUGIN DEFINITION
121 // ===========================
123 var old = $.fn.scrollspy
125 $.fn.scrollspy = function (option) {
126 return this.each(function () {
128 var data = $this.data('bs.scrollspy')
129 var options = typeof option == 'object' && option
131 if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
132 if (typeof option == 'string') data[option]()
136 $.fn.scrollspy.Constructor = ScrollSpy
139 // SCROLLSPY NO CONFLICT
140 // =====================
142 $.fn.scrollspy.noConflict = function () {
148 // SCROLLSPY DATA-API
149 // ==================
151 $(window).on('load', function () {
152 $('[data-spy="scroll"]').each(function () {
154 $spy.scrollspy($spy.data())