/** * Table of contents plugin for jQuery. * * Provides the possibility to automatically generate a toc, i.e., a table * of contents for a given element. All headers, i.e. hx for x in {1,...,6} * will be extracted and dynamically extented by hashes. Clicking on a link * in the toc will smoothly scroll to the corresponding headline. * * The plugin offers sufficient customization options through a set of * optional parameters. See the comments in the code and the example for * further information. * * Author: Jakob Bossek * Version: v1.1 */ (function($) { // FIXME: eventually add possibility to determine maximal depth // i.e., the maximal header level that is taken into account $.fn.toc = function(options) { // set default settings for plugin var settings = $.extend({ // option to shorten headlines that are too long shorten: false, // strip them after stripAfter: 50, // speed of scrolling animation scrollSpeed: 400, // offset (useful if fixed positioned headers are used) scrollOffset: 0, // wrapper for toc (for example if displayed in bubble) wrapWith: '
', // toc container (parent element of toc) container: 'body' }, options); // helper function for animation // FIXME: make plugin depend on smoothScroll plugin var scrollToHeadline = function(target) { // smoothly scroll to corresponding headline $("body, html").animate( {'scrollTop': $(target).offset().top - settings.scrollOffset}, settings.scrollSpeed, function() {} ); }; // iterate over all selected elements return this.each(function(index1) { // select all headers in current container var toc_container = $(settings.container), container = $(this), headlines = $(':header', container), toc = ''; toc_container.append($(toc)); $(".toc", toc_container).wrap(settings.wrapWith); // make the links scroll fine $("li a", $(".toc")).click(function(e) { // prevent link to behave as usual e.preventDefault(); // extract destination var target = $(e.target).attr('href'); scrollToHeadline(target); }); }); }; })(jQuery);