1 /* =============================================================
2 * bootstrap-scrollspy.js v2.0.4
3 * http://twitter.github.com/bootstrap/javascript.html#scrollspy
4 * =============================================================
5 * Copyright 2012 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 * ============================================================== */
23 "use strict"; // jshint ;_;
26 /* SCROLLSPY CLASS DEFINITION
27 * ========================== */
29 function ScrollSpy( element, options) {
30 var process = $.proxy(this.process, this)
31 , $element = $(element).is('body') ? $(window) : $(element)
33 this.options = $.extend({}, $.fn.scrollspy.defaults, options)
34 this.$scrollElement = $element.on('scroll.scroll.data-api', process)
35 this.selector = (this.options.target
36 || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
37 || '') + ' .nav li > a'
38 this.$body = $('body')
43 ScrollSpy.prototype = {
45 constructor: ScrollSpy
47 , refresh: function () {
58 , href = $el.data('target') || $el.attr('href')
59 , $href = /^#\w/.test(href) && $(href)
62 && [[ $href.position().top, href ]] ) || null
64 .sort(function (a, b) { return a[0] - b[0] })
66 self.offsets.push(this[0])
67 self.targets.push(this[1])
71 , process: function () {
72 var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
73 , scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
74 , maxScroll = scrollHeight - this.$scrollElement.height()
75 , offsets = this.offsets
76 , targets = this.targets
77 , activeTarget = this.activeTarget
80 if (scrollTop >= maxScroll) {
81 return activeTarget != (i = targets.last()[0])
82 && this.activate ( i )
85 for (i = offsets.length; i--;) {
86 activeTarget != targets[i]
87 && scrollTop >= offsets[i]
88 && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
89 && this.activate( targets[i] )
93 , activate: function (target) {
97 this.activeTarget = target
101 .removeClass('active')
103 selector = this.selector
104 + '[data-target="' + target + '"],'
105 + this.selector + '[href="' + target + '"]'
111 if (active.parent('.dropdown-menu')) {
112 active = active.closest('li.dropdown').addClass('active')
115 active.trigger('activate')
121 /* SCROLLSPY PLUGIN DEFINITION
122 * =========================== */
124 $.fn.scrollspy = function ( option ) {
125 return this.each(function () {
127 , data = $this.data('scrollspy')
128 , options = typeof option == 'object' && option
129 if (!data) $this.data('scrollspy', (data = new ScrollSpy(this, options)))
130 if (typeof option == 'string') data[option]()
134 $.fn.scrollspy.Constructor = ScrollSpy
136 $.fn.scrollspy.defaults = {
141 /* SCROLLSPY DATA-API
142 * ================== */
145 $('[data-spy="scroll"]').each(function () {
147 $spy.scrollspy($spy.data())