diff options
Diffstat (limited to 'assets/javascripts/bootstrap/scrollspy.js')
-rw-r--r-- | assets/javascripts/bootstrap/scrollspy.js | 260 |
1 files changed, 127 insertions, 133 deletions
diff --git a/assets/javascripts/bootstrap/scrollspy.js b/assets/javascripts/bootstrap/scrollspy.js index ac02443..e7d3a8b 100644 --- a/assets/javascripts/bootstrap/scrollspy.js +++ b/assets/javascripts/bootstrap/scrollspy.js @@ -7,170 +7,164 @@ * ======================================================================== */ -+function () { 'use strict'; - - (function (o_o) { - typeof define == 'function' && define.amd ? define(['jquery'], o_o) : - typeof exports == 'object' ? o_o(require('jquery')) : o_o(jQuery) - })(function ($) { ++function ($) { + 'use strict'; + + // SCROLLSPY CLASS DEFINITION + // ========================== + + function ScrollSpy(element, options) { + var process = $.proxy(this.process, this) + + this.$body = $('body') + this.$scrollElement = $(element).is('body') ? $(window) : $(element) + this.options = $.extend({}, ScrollSpy.DEFAULTS, options) + this.selector = (this.options.target || '') + ' .nav li > a' + this.offsets = [] + this.targets = [] + this.activeTarget = null + this.scrollHeight = 0 + + this.$scrollElement.on('scroll.bs.scrollspy', process) + this.refresh() + this.process() + } + + ScrollSpy.VERSION = '3.1.1' + + ScrollSpy.DEFAULTS = { + offset: 10 + } + + ScrollSpy.prototype.getScrollHeight = function () { + return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight) + } + + ScrollSpy.prototype.refresh = function () { + var offsetMethod = 'offset' + var offsetBase = 0 + + if (!$.isWindow(this.$scrollElement[0])) { + offsetMethod = 'position' + offsetBase = this.$scrollElement.scrollTop() + } - // SCROLLSPY CLASS DEFINITION - // ========================== + this.offsets = [] + this.targets = [] + this.scrollHeight = this.getScrollHeight() - function ScrollSpy(element, options) { - var process = $.proxy(this.process, this) + var self = this - this.$body = $('body') - this.$scrollElement = $(element).is('body') ? $(window) : $(element) - this.options = $.extend({}, ScrollSpy.DEFAULTS, options) - this.selector = (this.options.target || '') + ' .nav li > a' - this.offsets = [] - this.targets = [] - this.activeTarget = null - this.scrollHeight = 0 + this.$body + .find(this.selector) + .map(function () { + var $el = $(this) + var href = $el.data('target') || $el.attr('href') + var $href = /^#./.test(href) && $(href) - this.$scrollElement.on('scroll.bs.scrollspy', process) + return ($href + && $href.length + && $href.is(':visible') + && [[$href[offsetMethod]().top + offsetBase, href]]) || null + }) + .sort(function (a, b) { return a[0] - b[0] }) + .each(function () { + self.offsets.push(this[0]) + self.targets.push(this[1]) + }) + } + + ScrollSpy.prototype.process = function () { + var scrollTop = this.$scrollElement.scrollTop() + this.options.offset + var scrollHeight = this.getScrollHeight() + var maxScroll = this.options.offset + scrollHeight - this.$scrollElement.height() + var offsets = this.offsets + var targets = this.targets + var activeTarget = this.activeTarget + var i + + if (this.scrollHeight != scrollHeight) { this.refresh() - this.process() } - ScrollSpy.VERSION = '3.1.1' - - ScrollSpy.DEFAULTS = { - offset: 10 + if (scrollTop >= maxScroll) { + return activeTarget != (i = targets[targets.length - 1]) && this.activate(i) } - ScrollSpy.prototype.getScrollHeight = function () { - return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight) + if (activeTarget && scrollTop <= offsets[0]) { + return activeTarget != (i = targets[0]) && this.activate(i) } - ScrollSpy.prototype.refresh = function () { - var offsetMethod = 'offset' - var offsetBase = 0 - - if (!$.isWindow(this.$scrollElement[0])) { - offsetMethod = 'position' - offsetBase = this.$scrollElement.scrollTop() - } - - this.offsets = [] - this.targets = [] - this.scrollHeight = this.getScrollHeight() - - var self = this - - this.$body - .find(this.selector) - .map(function () { - var $el = $(this) - var href = $el.data('target') || $el.attr('href') - var $href = /^#./.test(href) && $(href) - - return ($href - && $href.length - && $href.is(':visible') - && [[$href[offsetMethod]().top + offsetBase, href]]) || null - }) - .sort(function (a, b) { return a[0] - b[0] }) - .each(function () { - self.offsets.push(this[0]) - self.targets.push(this[1]) - }) + for (i = offsets.length; i--;) { + activeTarget != targets[i] + && scrollTop >= offsets[i] + && (!offsets[i + 1] || scrollTop <= offsets[i + 1]) + && this.activate(targets[i]) } + } - ScrollSpy.prototype.process = function () { - var scrollTop = this.$scrollElement.scrollTop() + this.options.offset - var scrollHeight = this.getScrollHeight() - var maxScroll = this.options.offset + scrollHeight - this.$scrollElement.height() - var offsets = this.offsets - var targets = this.targets - var activeTarget = this.activeTarget - var i - - if (this.scrollHeight != scrollHeight) { - this.refresh() - } - - if (scrollTop >= maxScroll) { - return activeTarget != (i = targets[targets.length - 1]) && this.activate(i) - } - - if (activeTarget && scrollTop <= offsets[0]) { - return activeTarget != (i = targets[0]) && this.activate(i) - } - - for (i = offsets.length; i--;) { - activeTarget != targets[i] - && scrollTop >= offsets[i] - && (!offsets[i + 1] || scrollTop <= offsets[i + 1]) - && this.activate(targets[i]) - } - } + ScrollSpy.prototype.activate = function (target) { + this.activeTarget = target - ScrollSpy.prototype.activate = function (target) { - this.activeTarget = target + $(this.selector) + .parentsUntil(this.options.target, '.active') + .removeClass('active') - $(this.selector) - .parentsUntil(this.options.target, '.active') - .removeClass('active') + var selector = this.selector + + '[data-target="' + target + '"],' + + this.selector + '[href="' + target + '"]' - var selector = this.selector + - '[data-target="' + target + '"],' + - this.selector + '[href="' + target + '"]' + var active = $(selector) + .parents('li') + .addClass('active') - var active = $(selector) - .parents('li') + if (active.parent('.dropdown-menu').length) { + active = active + .closest('li.dropdown') .addClass('active') - - if (active.parent('.dropdown-menu').length) { - active = active - .closest('li.dropdown') - .addClass('active') - } - - active.trigger('activate.bs.scrollspy') } + active.trigger('activate.bs.scrollspy') + } - // SCROLLSPY PLUGIN DEFINITION - // =========================== - function Plugin(option) { - return this.each(function () { - var $this = $(this) - var data = $this.data('bs.scrollspy') - var options = typeof option == 'object' && option + // SCROLLSPY PLUGIN DEFINITION + // =========================== - if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options))) - if (typeof option == 'string') data[option]() - }) - } + function Plugin(option) { + return this.each(function () { + var $this = $(this) + var data = $this.data('bs.scrollspy') + var options = typeof option == 'object' && option - var old = $.fn.scrollspy + if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options))) + if (typeof option == 'string') data[option]() + }) + } - $.fn.scrollspy = Plugin - $.fn.scrollspy.Constructor = ScrollSpy + var old = $.fn.scrollspy + $.fn.scrollspy = Plugin + $.fn.scrollspy.Constructor = ScrollSpy - // SCROLLSPY NO CONFLICT - // ===================== - $.fn.scrollspy.noConflict = function () { - $.fn.scrollspy = old - return this - } + // SCROLLSPY NO CONFLICT + // ===================== + $.fn.scrollspy.noConflict = function () { + $.fn.scrollspy = old + return this + } - // SCROLLSPY DATA-API - // ================== - $(window).on('load.bs.scrollspy.data-api', function () { - $('[data-spy="scroll"]').each(function () { - var $spy = $(this) - Plugin.call($spy, $spy.data()) - }) - }) + // SCROLLSPY DATA-API + // ================== + $(window).on('load.bs.scrollspy.data-api', function () { + $('[data-spy="scroll"]').each(function () { + var $spy = $(this) + Plugin.call($spy, $spy.data()) + }) }) -}(); +}(jQuery); |