Skip to content

Instantly share code, notes, and snippets.

@chancesmith
Created October 13, 2016 14:20
Show Gist options
  • Select an option

  • Save chancesmith/ab3048302d521e445802dcafc41e9a25 to your computer and use it in GitHub Desktop.

Select an option

Save chancesmith/ab3048302d521e445802dcafc41e9a25 to your computer and use it in GitHub Desktop.
Adds top offset to all anchor links clicked (reads URL for anchor link and adds offset of scroll position)
// anchor links offset
$('a[href^="#"]').click(function() {
var target = $(this.hash);
if (target.length == 0) target = $('a[name="' + this.hash.substr(1) + '"]');
if (target.length == 0) target = $('html');
$('html, body').animate({ scrollTop: target.offset().top-170 }, 1000);
return false;
});
// Might need a mobile query to handle a different offset
// https://gist.github.com/chancesmith/332d5e312969623f34bf115dcedf4eae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment