comparison static/bootstrap/js/scrollspy.js @ 149:d29e2f337b00

Updated to Bootstrap 3.0.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 12 Dec 2013 21:54:04 -0800
parents
children
comparison
equal deleted inserted replaced
148:f02e049d6546 149:d29e2f337b00
1 /* ========================================================================
2 * Bootstrap: scrollspy.js v3.0.3
3 * http://getbootstrap.com/javascript/#scrollspy
4 * ========================================================================
5 * Copyright 2013 Twitter, Inc.
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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 * ======================================================================== */
19
20
21 +function ($) { "use strict";
22
23 // SCROLLSPY CLASS DEFINITION
24 // ==========================
25
26 function ScrollSpy(element, options) {
27 var href
28 var process = $.proxy(this.process, this)
29
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'
37 this.offsets = $([])
38 this.targets = $([])
39 this.activeTarget = null
40
41 this.refresh()
42 this.process()
43 }
44
45 ScrollSpy.DEFAULTS = {
46 offset: 10
47 }
48
49 ScrollSpy.prototype.refresh = function () {
50 var offsetMethod = this.$element[0] == window ? 'offset' : 'position'
51
52 this.offsets = $([])
53 this.targets = $([])
54
55 var self = this
56 var $targets = this.$body
57 .find(this.selector)
58 .map(function () {
59 var $el = $(this)
60 var href = $el.data('target') || $el.attr('href')
61 var $href = /^#\w/.test(href) && $(href)
62
63 return ($href
64 && $href.length
65 && [[ $href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
66 })
67 .sort(function (a, b) { return a[0] - b[0] })
68 .each(function () {
69 self.offsets.push(this[0])
70 self.targets.push(this[1])
71 })
72 }
73
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
81 var i
82
83 if (scrollTop >= maxScroll) {
84 return activeTarget != (i = targets.last()[0]) && this.activate(i)
85 }
86
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] )
92 }
93 }
94
95 ScrollSpy.prototype.activate = function (target) {
96 this.activeTarget = target
97
98 $(this.selector)
99 .parents('.active')
100 .removeClass('active')
101
102 var selector = this.selector
103 + '[data-target="' + target + '"],'
104 + this.selector + '[href="' + target + '"]'
105
106 var active = $(selector)
107 .parents('li')
108 .addClass('active')
109
110 if (active.parent('.dropdown-menu').length) {
111 active = active
112 .closest('li.dropdown')
113 .addClass('active')
114 }
115
116 active.trigger('activate.bs.scrollspy')
117 }
118
119
120 // SCROLLSPY PLUGIN DEFINITION
121 // ===========================
122
123 var old = $.fn.scrollspy
124
125 $.fn.scrollspy = function (option) {
126 return this.each(function () {
127 var $this = $(this)
128 var data = $this.data('bs.scrollspy')
129 var options = typeof option == 'object' && option
130
131 if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
132 if (typeof option == 'string') data[option]()
133 })
134 }
135
136 $.fn.scrollspy.Constructor = ScrollSpy
137
138
139 // SCROLLSPY NO CONFLICT
140 // =====================
141
142 $.fn.scrollspy.noConflict = function () {
143 $.fn.scrollspy = old
144 return this
145 }
146
147
148 // SCROLLSPY DATA-API
149 // ==================
150
151 $(window).on('load', function () {
152 $('[data-spy="scroll"]').each(function () {
153 var $spy = $(this)
154 $spy.scrollspy($spy.data())
155 })
156 })
157
158 }(jQuery);