(function($) { $.fn.toc = function(options) { var self = this; var opts = $.extend({}, jQuery.fn.toc.defaults, options); var container = $(opts.container); var headings = $(opts.selectors, container); var headingOffsets = []; var activeClassName = opts.prefix + '-active'; var navbarHeight = $('.navbar').height(); var scrollTo = function(e) { if (opts.smoothScrolling) { e.preventDefault(); var elScrollTo = $(e.target).attr('href'); var $el = $(elScrollTo); var offsetTop = $el.offset().top - navbarHeight; $('body,html').animate({ scrollTop: offsetTop }, 400, 'swing', function() { location.hash = elScrollTo; }); } $('li', self).removeClass(activeClassName); $(e.target).parent().addClass(activeClassName); }; //highlight on scroll var timeout; var highlightOnScroll = function(e) { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(function() { var top = $(window).scrollTop(), highlighted; for (var i = 0, c = headingOffsets.length; i < c; i++) { if (headingOffsets[i] >= top) { $('li', self).removeClass(activeClassName); if (i > 0) { highlighted = $('li:eq(' + (i - 1) + ')', self).addClass(activeClassName); opts.onHighlight(highlighted); } break; } } }, 50); }; if (opts.highlightOnScroll) { $(window).bind('scroll', highlightOnScroll); highlightOnScroll(); } return this.each(function() { //build TOC var el = $(this); var ul = $('