Created
March 25, 2011 00:27
-
-
Save haggen/886155 to your computer and use it in GitHub Desktop.
Make hash anchors scroll smoothly
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
/** | |
* 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