/* ============================================================= * bootstrap-scrollspy.js v2.0.1 * http://twitter.github.com/bootstrap/javascript.html#scrollspy * ============================================================= * copyright 2012 twitter, inc. * * licensed under the apache license, version 2.0 (the "license"); * you may not use this file except in compliance with the license. * you may obtain a copy of the license at * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in writing, software * distributed under the license is distributed on an "as is" basis, * without warranties or conditions of any kind, either express or implied. * see the license for the specific language governing permissions and * limitations under the license. * ============================================================== */ !function ( $ ) { "use strict" /* scrollspy class definition * ========================== */ function scrollspy( element, options) { var process = $.proxy(this.process, this) , $element = $(element).is('body') ? $(window) : $(element) , href this.options = $.extend({}, $.fn.scrollspy.defaults, options) this.$scrollelement = $element.on('scroll.scroll.data-api', process) this.selector = (this.options.target || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7 || '') + ' .nav li > a' this.$body = $('body').on('click.scroll.data-api', this.selector, process) this.refresh() this.process() } scrollspy.prototype = { constructor: scrollspy , refresh: function () { this.targets = this.$body .find(this.selector) .map(function () { var href = $(this).attr('href') return /^#\w/.test(href) && $(href).length ? href : null }) this.offsets = $.map(this.targets, function (id) { return $(id).position().top }) } , process: function () { var scrolltop = this.$scrollelement.scrolltop() + this.options.offset , offsets = this.offsets , targets = this.targets , activetarget = this.activetarget , i for (i = offsets.length; i--;) { activetarget != targets[i] && scrolltop >= offsets[i] && (!offsets[i + 1] || scrolltop <= offsets[i + 1]) && this.activate( targets[i] ) } } , activate: function (target) { var active this.activetarget = target this.$body .find(this.selector).parent('.active') .removeclass('active') active = this.$body .find(this.selector + '[href="' + target + '"]') .parent('li') .addclass('active') if ( active.parent('.dropdown-menu') ) { active.closest('li.dropdown').addclass('active') } } } /* scrollspy plugin definition * =========================== */ $.fn.scrollspy = function ( option ) { return this.each(function () { var $this = $(this) , data = $this.data('scrollspy') , options = typeof option == 'object' && option if (!data) $this.data('scrollspy', (data = new scrollspy(this, options))) if (typeof option == 'string') data[option]() }) } $.fn.scrollspy.constructor = scrollspy $.fn.scrollspy.defaults = { offset: 10 } /* scrollspy data-api * ================== */ $(function () { $('[data-spy="scroll"]').each(function () { var $spy = $(this) $spy.scrollspy($spy.data()) }) }) }( window.jquery );