Last active
December 15, 2015 14:39
-
-
Save orioltf/5275849 to your computer and use it in GitHub Desktop.
#CODEPEN #JQUERY: Automatic Table of Contents
http://codepen.io/orioltf/pen/ijEFq
http://codepen.io/chriscoyier/pen/EnLwb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<article> | |
<h1>Documentation</h1> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<div class="all-questions"> | |
<h3 id="one">How do you smurf a murf?</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="two">How do many licks does a giraffe?</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="three">Which Frank is?</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="four">Is fourteen enough?</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="five">Is a circle an oval y/n?</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="six">Many mongerals manifest mountains</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="seven">How would you like a soda drink?</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="eight">I would very much like a soda drink.</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
<h3 id="nine">Thank you for accepting the soda drink.</h3> | |
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
</div> | |
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ToC = | |
'<nav role="navigation" class="table-of-contents">' + | |
'<h2>On this page:</h2>' + | |
'<ul>'; | |
var newLine, el, title, link; | |
$('article h3').each(function() { | |
el = $(this); | |
title = el.text(); | |
link = "#" + el.attr("id"); | |
newLine = | |
'<li>' + | |
'<a href="' + link + '">' + | |
title + | |
'</a>' + | |
'</li>'; | |
ToC += newLine; | |
}); | |
ToC += | |
'</ul>' + | |
'</nav>'; | |
$('.all-questions').prepend(ToC); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background: #eee; | |
padding: 20px; | |
} | |
article { | |
max-width: 50em; | |
background: white; | |
padding: 2em; | |
margin: 1em auto; | |
} | |
.table-of-contents { | |
float: right; | |
width: 40%; | |
background: #eee; | |
font-size: 0.8em; | |
padding: 1em 2em; | |
margin: 0 0 0.5em 0.5em; | |
} | |
.table-of-contents ul { | |
padding: 0; | |
} | |
.table-of-contents li { | |
margin: 0 0 0.25em 0; | |
} | |
.table-of-contents a { | |
text-decoration: none; | |
} | |
.table-of-contents a:hover, | |
.table-of-contents a:active { | |
text-decoration: underline; | |
} | |
h3:target { | |
animation: highlight 1s ease; | |
} | |
@keyframes highlight { | |
from { background: yellow; } | |
to { background: white; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment