Created
April 27, 2012 18:02
-
-
Save nathos/2511377 to your computer and use it in GitHub Desktop.
jQuery Smooth Scrolling Internal Links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function($) { | |
// from http://imakewebthings.com/jquery-waypoints/ | |
// 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; | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment