Skip to content

Instantly share code, notes, and snippets.

@lukewhitehouse
Created September 25, 2015 14:44
Show Gist options
  • Save lukewhitehouse/50777eb273e99d9137b0 to your computer and use it in GitHub Desktop.
Save lukewhitehouse/50777eb273e99d9137b0 to your computer and use it in GitHub Desktop.
Scroll to element on click

Scroll to element on click

This script gets the href attribute from the element and scroll the window down to that location.

$('.anchor').on('click', function(e) {
var scrollLocation = $(this).attr('href');
$('html, body').animate({
scrollTop: $(scrollLocation).offset().top*.7
}, 550);
if(history.pushState) {
history.pushState(null, null, scrollLocation);
} else {
location.hash = scrollLocation;
}
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment