Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Last active January 7, 2016 12:30
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 mariovalney/ffce3fbe327246947ca9 to your computer and use it in GitHub Desktop.
Save mariovalney/ffce3fbe327246947ca9 to your computer and use it in GitHub Desktop.
Scroller
$.fn.scroller = function(offsetTop) {
if (offsetTop == null) {
offsetTop = 0;
}
this.each( function() {
var id = this.id;
$("a[href=#" + id + "]").on('click', function(event) {
event.preventDefault();
var distanceToTop = $(document.getElementById(id)).offset().top - offsetTop;
if (distanceToTop != null) {
$('html, body').stop().animate({ scrollTop: distanceToTop }, 1000);
}
});
});
};
// USO: coloque o ID do alvo da âncora no href do link <a class="link-com-ancora" href="#id">ancora</a> e use o .scroller();
jQuery(document).ready(function($) {
$('.link-com-ancora').scroller();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment