Last active
January 7, 2016 12:30
-
-
Save mariovalney/ffce3fbe327246947ca9 to your computer and use it in GitHub Desktop.
Scroller
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
$.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