Skip to content

Instantly share code, notes, and snippets.

@watershed
Created February 9, 2011 12:58
Show Gist options
  • Save watershed/818430 to your computer and use it in GitHub Desktop.
Save watershed/818430 to your computer and use it in GitHub Desktop.
Add skip to section links
// Add skip links to sections
SECT.add_skip = function() {
// Create a placeholder we can append to
var $skip = $('<div class="skip"></div>');
$('.body .section').each(function(){
_section = $(this);
// Check the section has an id we can link to and a subheading
if (_section.attr('id').length && (_section.find('h2').length > 0) ) {
// Check if section is below the fold
if ( _section.offset().top > $(window).height() ) {
// Append a link to our placeholder
$link = $('<a href=""></a>')
.attr('href','#' + _section.attr('id'))
.text(_section.find('h2:first').text())
// .click(get localScroll working here!)
;
$skip.append($link);
}
}
});
// Delimit the list of links
if ($skip.text().length > 0) {
var $count = $skip.find('a').length
if ($count > 1) {
$skip.find('a:last').before(' or ');
}
if ($count > 2) {
var $n = $count - 1
$skip.find('a:lt(' + $n + ')').before('<span class="delim">, </span>');
}
// Change the text before the first link
$skip.find('.delim:first').text('Skip to ');
// Insert the placeholder
$('#options ul').before($skip);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment