Skip to content

Instantly share code, notes, and snippets.

@benjamincharity
Forked from cange/smooscroll.js
Created May 9, 2012 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamincharity/2647337 to your computer and use it in GitHub Desktop.
Save benjamincharity/2647337 to your computer and use it in GitHub Desktop.
Simple smooth scrolling solution
// <a rel="smoothscroll" href="#foo">got to foo</a>
// <div id="foo">Foo content</div>
$(function () {
$('a[rel=smoothscroll]').click(function( event ) {
var location = window.location,
hash = $(this).attr('href')
;
$('html, body')
.stop()
.animate({scrollTop: $(hash).offset().top}, 800, function () {
window.location = location.pathname + location.search + hash;
})
;
event.preventDefault();
});
var hash = window.location.hash;
if (!!hash) {
$('a[rel=smoothscroll][href=' + hash + ']').click();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment