Skip to content

Instantly share code, notes, and snippets.

@rbrunt
Created August 9, 2012 09:32
Show Gist options
  • Select an option

  • Save rbrunt/3302677 to your computer and use it in GitHub Desktop.

Select an option

Save rbrunt/3302677 to your computer and use it in GitHub Desktop.
Using #section links with scrollto jquery plugin without any messy jumpiness :)
$(document).ready(function(){
$('nav a').click(function(e){
// If a link has been clicked, scroll the page to the link's hash target:
// Links just need their href to be the hash of the section you're scrolling to e.g. #top, no class to choose them...
// This has the advantage that there is no loss in functionality from users without javascript
$.scrollTo( this.hash || 0, 300);
// Update the URL in the browser to the new anchor point
history.pushState(null, null, this.href);
// Prevents the browser actually following the link - no jumping down before scrolling :)
e.preventDefault();
});
});
@rbrunt

rbrunt commented Aug 9, 2012

Copy link
Copy Markdown
Author

e.preventDefault() prevents the browser from updating the url bar

@npfedwards

Copy link
Copy Markdown

You used it anywhere to check out? Currently using scrollTo but not in accessible way for non js support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment