1 /* ========================================================================
2 * Bootstrap: scrollspy.js v3.3.7
3 * http://getbootstrap.com/javascript/#scrollspy
4 * ========================================================================
5 * Copyright 2011-2016 Twitter, Inc.
6 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7 * ======================================================================== */
13 // SCROLLSPY CLASS DEFINITION
14 // ==========================
16 function ScrollSpy(element, options) {
17 this.$body = $(document.body)
18 this.$scrollElement = $(element).is(document.body) ? $(window) : $(element)
19 this.options = $.extend({}, ScrollSpy.DEFAULTS, options)
20 this.selector = (this.options.target || '') + ' .nav li > a'
23 this.activeTarget = null
26 this.$scrollElement.on('scroll.bs.scrollspy', $.proxy(this.process, this))
31 ScrollSpy.VERSION = '3.3.7'
33 ScrollSpy.DEFAULTS = {
37 ScrollSpy.prototype.getScrollHeight = function () {
38 return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight)
41 ScrollSpy.prototype.refresh = function () {
43 var offsetMethod = 'offset'
48 this.scrollHeight = this.getScrollHeight()
50 if (!$.isWindow(this.$scrollElement[0])) {
51 offsetMethod = 'position'
52 offsetBase = this.$scrollElement.scrollTop()
59 var href = $el.data('target') || $el.attr('href')
60 var $href = /^#./.test(href) && $(href)
64 && $href.is(':visible')
65 && [[$href[offsetMethod]().top + offsetBase, href]]) || null
67 .sort(function (a, b) { return a[0] - b[0] })
69 that.offsets.push(this[0])
70 that.targets.push(this[1])
74 ScrollSpy.prototype.process = function () {
75 var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
76 var scrollHeight = this.getScrollHeight()
77 var maxScroll = this.options.offset + scrollHeight - this.$scrollElement.height()
78 var offsets = this.offsets
79 var targets = this.targets
80 var activeTarget = this.activeTarget
83 if (this.scrollHeight != scrollHeight) {
87 if (scrollTop >= maxScroll) {
88 return activeTarget != (i = targets[targets.length - 1]) && this.activate(i)
91 if (activeTarget && scrollTop < offsets[0]) {
92 this.activeTarget = null
96 for (i = offsets.length; i--;) {
97 activeTarget != targets[i]
98 && scrollTop >= offsets[i]
99 && (offsets[i + 1] === undefined || scrollTop < offsets[i + 1])
100 && this.activate(targets[i])
104 ScrollSpy.prototype.activate = function (target) {
105 this.activeTarget = target
109 var selector = this.selector +
110 '[data-target="' + target + '"],' +
111 this.selector + '[href="' + target + '"]'
113 var active = $(selector)
117 if (active.parent('.dropdown-menu').length) {
119 .closest('li.dropdown')
123 active.trigger('activate.bs.scrollspy')
126 ScrollSpy.prototype.clear = function () {
128 .parentsUntil(this.options.target, '.active')
129 .removeClass('active')
133 // SCROLLSPY PLUGIN DEFINITION
134 // ===========================
136 function Plugin(option) {
137 return this.each(function () {
139 var data = $this.data('bs.scrollspy')
140 var options = typeof option == 'object' && option
142 if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
143 if (typeof option == 'string') data[option]()
147 var old = $.fn.scrollspy
149 $.fn.scrollspy = Plugin
150 $.fn.scrollspy.Constructor = ScrollSpy
153 // SCROLLSPY NO CONFLICT
154 // =====================
156 $.fn.scrollspy.noConflict = function () {
162 // SCROLLSPY DATA-API
163 // ==================
165 $(window).on('load.bs.scrollspy.data-api', function () {
166 $('[data-spy="scroll"]').each(function () {
168 Plugin.call($spy, $spy.data())