Skip to content

Instantly share code, notes, and snippets.

@haggen
Created March 25, 2011 00:27
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 haggen/886155 to your computer and use it in GitHub Desktop.
Save haggen/886155 to your computer and use it in GitHub Desktop.
Make hash anchors scroll smoothly
/**
* Hookshot v1.1
* jQuery plugin for fragment anchors to scroll smoothly
* by Arthur Corenzan <arthur@corenzan.com>
* Creative Commons-Attribution-Share Alike
* http://creativecommons.org/licenses/by-sa/3.0
*
* Usage:
*
* Target the anchors you want to enable smoothly
* scrolling and apply hookshot on them
*
* <a href="#content">Skip to content</p>
*
* $("a[href=^a]").hookshot({});
*
* Options:
*
* speed: integer, milliseconds the animation takes
*/
(function($) {
$.fn.hookshot = function(o) {
o = $.extend({
speed: 200
}, o);
return this.each(function(i, that) {
$(that).click(function(e) {
e.preventDefault();
var n, target;
target = this.href.split('#').pop();
n = target ? $('#' + target).offset().top : 0;
$('html, body').animate({'scrollTop': n}, o.speed);
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment