Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="toc"></div>













<div class="section" style="background: #abe4ff;">
<h2>Features</h2>
<ul>
<li>Completely customizable</li>
<li>Click to smooth scroll to that spot on the page</li>
<li>Automatically highlight the current section</li>
<li>Extremely lightweight (744 bytes gzipped)</li>
<li>Can have multiple on a page</li>
</ul>
</div>


<div class="section" style="background: #ffedab;">
<h2>Download</h2>
<ul>
<li><a href="https://raw.github.com/jgallen23/toc/master/dist/toc.min.js">Production</a></li>
<li><a href="https://raw.github.com/jgallen23/toc/master/dist/toc.js">Development</a></li>
<li><a href="https://github.com/jgallen23/toc">Source</a></li>
</ul>  
</div>


<div class="section" style="background: #ffc9fb;">
<h2>Usage</h2>
<pre><code>$(<span class="string">'#toc'</span>).toc();</code></pre>
</div> 
  
  
  
  
<div class="section" style="background: #caffc9;">  
<h3>Options</h3>
<p>Defaults shown below
    </p></div>




              
            
!

CSS

              
                #toc {
    top: 0px;
    left: 0px;
    height: 100%;
    position: fixed;
    background: #333;
    box-shadow: inset -5px 0 5px 0px #000;
    width: 150px;
    padding-top: 20px;
    color: #fff;
}

#toc ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

#toc li {
    padding: 5px 10px;
}

#toc a {
    color: #fff;
    text-decoration: none;
    display: block;
}

#toc .toc-h2 {
    padding-left: 10px;
}

#toc .toc-h3 {
    padding-left: 20px;
}

#toc .toc-active {
    background: #336699;
    box-shadow: inset -5px 0px 10px -5px #000;
}





h2, h3{
  font-size: 60px;
}
.section{
  height: 900px;
  margin: -50px 0 0 160px;
}
              
            
!

JS

              
                
/*!
 * toc - jQuery Table of Contents Plugin
 * v0.3.2
 * http://projects.jga.me/toc/
 * copyright Greg Allen 2014
 * MIT License
 */
/*!
 * smooth-scroller - Javascript lib to handle smooth scrolling
 * v0.1.2
 * https://github.com/firstandthird/smooth-scroller
 * copyright First+Third 2014
 * MIT License
 */
//smooth-scroller.js

(function($) {
    $.fn.smoothScroller = function(options) {
        options = $.extend({}, $.fn.smoothScroller.defaults, options);
        var el = $(this);

        $(options.scrollEl).animate({
            scrollTop: el.offset().top - $(options.scrollEl).offset().top - options.offset
        }, options.speed, options.ease, function() {
            var hash = el.attr('id');

            if (hash.length) {
                if (history.pushState) {
                    history.pushState(null, null, '#' + hash);
                } else {
                    document.location.hash = hash;
                }
            }

            el.trigger('smoothScrollerComplete');
        });

        return this;
    };

    $.fn.smoothScroller.defaults = {
        speed: 400,
        ease: 'swing',
        scrollEl: 'body,html',
        offset: 0
    };

    $('body').on('click', '[data-smoothscroller]', function(e) {
        e.preventDefault();
        var href = $(this).attr('href');

        if (href.indexOf('#') === 0) {
            $(href).smoothScroller();
        }
    });
}(jQuery));

(function($) {
    var verboseIdCache = {};
    $.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.activeClass;

        var scrollTo = function(e, callback) {
            if (opts.smoothScrolling && typeof opts.smoothScrolling === 'function') {
                e.preventDefault();
                var elScrollTo = $(e.target).attr('href');

                opts.smoothScrolling(elScrollTo, opts, callback);
            }
            $('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, closest = Number.MAX_VALUE,
                    index = 0;

                for (var i = 0, c = headingOffsets.length; i < c; i++) {
                    var currentClosest = Math.abs(headingOffsets[i] - top);
                    if (currentClosest < closest) {
                        index = i;
                        closest = currentClosest;
                    }
                }

                $('li', self).removeClass(activeClassName);
                highlighted = $('li:eq(' + index + ')', self).addClass(activeClassName);
                opts.onHighlight(highlighted);
            }, 50);
        };
        if (opts.highlightOnScroll) {
            $(window).bind('scroll', highlightOnScroll);
            highlightOnScroll();
        }

        return this.each(function() {
            //build TOC
            var el = $(this);
            var ul = $(opts.listType);

            headings.each(function(i, heading) {
                var $h = $(heading);
                headingOffsets.push($h.offset().top - opts.highlightOffset);

                var anchorName = opts.anchorName(i, heading, opts.prefix);

                //add anchor
                if (heading.id !== anchorName) {
                    var anchor = $('<span/>').attr('id', anchorName).insertBefore($h);
                }

                //build TOC item
                var a = $('<a/>')
                    .text(opts.headerText(i, heading, $h))
                    .attr('href', '#' + anchorName)
                    .bind('click', function(e) {
                    $(window).unbind('scroll', highlightOnScroll);
                    scrollTo(e, function() {
                        $(window).bind('scroll', highlightOnScroll);
                    });
                    el.trigger('selected', $(this).attr('href'));
                });

                var li = $('<li/>')
                    .addClass(opts.itemClass(i, heading, $h, opts.prefix))
                    .append(a);

                ul.append(li);
            });
            el.html(ul);
        });
    };


    jQuery.fn.toc.defaults = {
        container: 'body',
        listType: '<ul/>',
        selectors: 'h1,h2,h3',
        smoothScrolling: function(target, options, callback) {
            $(target).smoothScroller({
                offset: options.scrollToOffset
            }).on('smoothScrollerComplete', function() {
                callback();
            });
        },
        scrollToOffset: 0,
        prefix: 'toc',
        activeClass: 'toc-active',
        onHighlight: function() {},
        highlightOnScroll: true,
        highlightOffset: 100,
        anchorName: function(i, heading, prefix) {
            if (heading.id.length) {
                return heading.id;
            }

            var candidateId = $(heading).text().replace(/[^a-z0-9]/ig, ' ').replace(/\s+/g, '-').toLowerCase();
            if (verboseIdCache[candidateId]) {
                var j = 2;

                while (verboseIdCache[candidateId + j]) {
                    j++;
                }
                candidateId = candidateId + '-' + j;

            }
            verboseIdCache[candidateId] = true;

            return prefix + '-' + candidateId;
        },
        headerText: function(i, heading, $heading) {
            return $heading.text();
        },
        itemClass: function(i, heading, $heading, prefix) {
            return prefix + '-' + $heading[0].tagName.toLowerCase();
        }

    };

})(jQuery); 


$('#toc').toc({
    'selectors': 'h1,h2,h3', //elements to use as headings
    'container': 'body', //element to find all selectors in
    'smoothScrolling': true, //enable or disable smooth scrolling on click
    'prefix': 'toc', //prefix for anchor tags and class names
    'onHighlight': function(el) {}, //called when a new section is highlighted 
    'highlightOnScroll': true, //add class to heading that is currently in focus
    'highlightOffset': 1, //offset to trigger the next headline
    'anchorName': function(i, heading, prefix) { //custom function for anchor name
        return prefix+i;
    },
    'headerText': function(i, heading, $heading) { //custom function building the header-item text
        return $heading.text();
    },
'itemClass': function(i, heading, $heading, prefix) { // custom function for item class
  return $heading[0].tagName.toLowerCase();
}
});
              
            
!
999px

Console