Skip to content

Instantly share code, notes, and snippets.

@ork
Last active December 23, 2015 08:29
Show Gist options
  • Save ork/6608405 to your computer and use it in GitHub Desktop.
Save ork/6608405 to your computer and use it in GitHub Desktop.
Add an anchor link reference to section headers.
[].forEach.call(document.querySelectorAll('h1,h2'), function(el) {
var section_anchor = document.createElement('a');
section_anchor.setAttribute('class', 'section_anchor');
section_anchor.setAttribute('title', 'Permalink');
section_anchor.setAttribute('href', '#' + el.id);
section_anchor.appendChild(document.createTextNode('¶'));
section_anchor.style.visibility = 'hidden';
el.appendChild(section_anchor);
el.addEventListener('mouseover', function() {
section_anchor.style.visibility = 'visible';
}, false);
el.addEventListener('mouseout', function() {
section_anchor.style.visibility = 'hidden';
}, false);
});
/* I used this (ugly) jQuery version first, but using jQuery only for this wasn't worth it */
$('h1,h2').each(function() {
$('<a class="section_anchor">&para;</a>').attr('title', 'Permalien').attr('href', '#' + this.id).appendTo(this).hide();
$(this).hover(function() {
$(this).find('a').show();
}, function() {
$(this).find('a').hide();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment