Skip to content

Instantly share code, notes, and snippets.

@helisoncruz
Created November 19, 2014 14:29
Show Gist options
  • Save helisoncruz/8d3415246726b6f73d8c to your computer and use it in GitHub Desktop.
Save helisoncruz/8d3415246726b6f73d8c to your computer and use it in GitHub Desktop.
Scroll suave com Jquery
//NA SUA PÁGINA minha.php
$(function(){
$('.scroll').click(function(e){
e.preventDefault();
});
$('#nav ul li a').smoothScroll();
});
//NO ARQUIVO smoothScroll.js
jQuery.fn.smoothScroll = function(){
$(this).each(function(){
var node = $(this);
$(node).click(function(e){
var anchor = $(this).attr('href');
anchor = anchor.split("#");
anchor = anchor[1];
var t = 0;
var found = false;
var tName = 'a[name='+anchor+']';
var tId = '#'+anchor;
if (!!$(tName).length){
t = $(tName).offset().top;
if ($(tName).text() == ""){
t = $(tName).parent().offset().top;
}
found = true;
} else if(!!$(tId).length){
t = $(tId).offset().top;
found = true;
}
if (found){
$("body, html").animate({scrollTop: t}, 1500);
}
//e.preventDefault();
});
});
var lAnchor = location.hash;
if (lAnchor.length > 0){
lAnchor = lAnchor.split("#");
lAnchor = lAnchor[1];
if (lAnchor.length > 0){
$("body, html").scrollTop(0);
var lt = 0;
var lfound = false;
var ltName = 'a[name='+lAnchor+']';
var ltId = '#'+lAnchor;
if (!!$(ltName).length){
lt = $(ltName).offset().top;
if ($(ltName).text() == ""){
lt = $(ltName).parent().offset().top;
}
lfound = true;
} else if(!!$(ltId).length){
lt = $(ltId).offset().top;
lfound = true;
}
if (lfound){
$("body, html").animate({scrollTop: lt}, 1500);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment