Created
September 10, 2013 15:41
-
-
Save lionhylra/6511281 to your computer and use it in GitHub Desktop.
jQuery.scrollto.js
This file contains hidden or 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.scrollto.js 0.0.1 - https://github.com/yckart/jquery.scrollto.js | |
* Scroll smooth to any element in your DOM. | |
* | |
* Copyright (c) 2012 Yannick Albert (http://yckart.com) | |
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php). | |
* 2013/02/17 | |
**/ | |
$.scrollTo = $.fn.scrollTo = function(x, y, options){ | |
if (!(this instanceof $)) return $.fn.scrollTo.apply($('html, body'), arguments); | |
options = $.extend({}, { | |
gap: { | |
x: 0, | |
y: 0 | |
}, | |
animation: { | |
easing: 'swing', | |
duration: 600, | |
complete: $.noop, | |
step: $.noop | |
} | |
}, options); | |
return this.each(function(){ | |
var elem = $(this); | |
elem.stop().animate({ | |
scrollLeft: !isNaN(Number(x)) ? x : $(y).offset().left + options.gap.x, | |
scrollTop: !isNaN(Number(y)) ? y : $(y).offset().top + options.gap.y | |
}, options.animation); | |
}); | |
}; |
This file contains hidden or 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
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script> | |
<script src='jquery.scrollto.js'></script> | |
<script> | |
$('a').click(function(e){ | |
$('html,body').scrollTo(this.hash, this.hash); | |
e.preventDefault(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment