Skip to content

Instantly share code, notes, and snippets.

@mertenvg
Created July 27, 2014 14:02
Show Gist options
  • Save mertenvg/fcd075236b0229d14832 to your computer and use it in GitHub Desktop.
Save mertenvg/fcd075236b0229d14832 to your computer and use it in GitHub Desktop.
Scroll to anchor with same name as current page. Uses jQuery.
/*
* Scroll to page section if it exists, but delay for a bit first.
*/
var page = location.href.replace(/^.+?\/?([^\/]+)$/i, '$1')
, delay = 500
, offset = -50
, $anchor;
if (page && ($anchor = $('a[name="'+page+'"]')).length) {
$('html,body').delay(delay).animate({scrollTop: $anchor.offset().top + offset}, 'slow');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment