Last active
December 23, 2015 08:29
-
-
Save ork/6608405 to your computer and use it in GitHub Desktop.
Add an anchor link reference to section headers.
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
[].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); | |
}); |
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
/* 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">¶</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