Skip to content

Instantly share code, notes, and snippets.

@jworksuk
Created April 30, 2013 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jworksuk/5487481 to your computer and use it in GitHub Desktop.
Save jworksuk/5487481 to your computer and use it in GitHub Desktop.
Smooth scrolling for internal links
// Wicked credit to
// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
var scrollElement = 'html, body';
$('html, body').each(function () {
var initScrollTop = $(this).attr('scrollTop');
$(this).attr('scrollTop', initScrollTop + 1);
if ($(this).attr('scrollTop') == initScrollTop + 1) {
scrollElement = this.nodeName.toLowerCase();
$(this).attr('scrollTop', initScrollTop);
return false;
}
});
// Smooth scrolling for internal links
$("a[href^='#']").click(function(event) {
event.preventDefault();
var $this = $(this),
target = this.hash,
$target = $(target);
$(scrollElement).stop().animate({
'scrollTop': $target.offset().top
}, 500, 'swing', function() {
//window.location.hash = target;
});
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment