Link to Heading
by rodneyrehm
HTML
<script src="http://leaverou.github.com/prefixfree/prefixfree.min.js"></script>
<header><p>Hover over the headlines to get a "link-to-section" link</p></header>
<!-- start slipsum code -->
<h1 id="I'm serious as a heart attack">I'm serious as a heart attack</h1>
<p>Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb.</p>
<p>Look, just because I don't be givin' no man a foot massage don't make it right for Marsellus to throw Antwone into a glass motherfuckin' house, fuckin' up the way the nigger talks. Motherfucker do that shit to me, he better paralyze my ass, 'cause I'll kill the motherfucker, know what I'm sayin'?</p>
<h2 id="dead">Is she dead, yes or no?</h2>
<p>Normally, both your asses would be dead as fucking fried chicken, but you happen to pull this shit while I'm in a transitional period so I don't wanna kill you, I wanna help you. But I can't give you this case, it don't belong to me. Besides, I've already been through too much shit this morning over this case to hand it over to your dumb ass.</p>
<h3 id="happy">We happy?</h3>
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows. Some pilots get picked and become television programs. Some don't, become nothing. She starred in one of the ones that became nothing.</p>
<h4 id="piss">I gotta piss</h4>
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they...
CSS
body {
padding: 40px;
font-size: 16px;
}
a {
color: #CCC;
}
h1, h2, h3, h4, h5, h6, p {
margin: 10px 0 10px 0;
position: relative;
}
h1 { font-size: 1.3em; }
h2 { font-size: 1.25em; }
h3 { font-size: 1.2em; }
h4 { font-size: 1.15em; }
h5 { font-size: 1.1em; }
h6 { font-size: 1.05em; }
h1:before { content: "h1"; position: absolute; left: -30px; color: gray; }
h2:before { content: "h2"; position: absolute; left: -30px; color: gray; }
h3:before { content: "h3"; position: absolute; left: -30px; color: gray; }
h4:before { content: "h4"; position: absolute; left: -30px; color: gray; }
h5:before { content: "h5"; position: absolute; left: -30px; color: gray; }
h6:before { content: "h6"; position: absolute; left: -30px; color: gray; }
#anchorLink {
display: inline-block;
margin-left: 10px;
color: #0090D2;
font-weight: bold;
text-decoration: none;
/* https://github.com/LeaVerou/prefixfree */
transition: all 0.25s ease-in;
}
#anchorLink.invisible {
opacity: 0;
}
#anchorLink:hover {
color: #0063A6;
text-decoration: none;
}
header > p,
footer > p {
color: #FFF;
background: #333;
padding: 20px 40px;
margin: -20px -40px 20px -40px;
}
footer > p {
margin: 20px -40px -20px -40px;
}
JavaScript
var anchorTransitionEnd = function(e) {
$(this).remove();
},
events = {
mouseenter: function(e) {
if (!this.id) {
return;
}
$('#anchorLink').remove();
var $this = $(this);
var $anchor = $('<a id="anchorLink" class="invisible" title="Link to Heading">#</a>')
.attr('href', '#' + this.id)
.appendTo($this);
$anchor.css('top'); // force reflow
$anchor.removeClass('invisible');
},
mouseleave: function(e) {
if (!this.id) {
return;
}
var $this = $(this),
$anchor = $this.find('#anchorLink');
if (e.targetElemenet === $anchor.get(0)) {
return;
}
$anchor.bind("webkitTransitionEnd oTransitionEnd transitionend", anchorTransitionEnd).addClass('invisible');
}
};
$(document.body).on(events, 'h1, h2, h3, h4, h5, h6');