Created
April 26, 2015 11:02
-
-
Save nicoknoll/25604b7785b83eee94be to your computer and use it in GitHub Desktop.
Smooth scroll if #hash is on page
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
$(document).ready(function(){ | |
$('a[href*="#"]').on('click',function (e) { | |
if(!($(this.hash).length)) return true; | |
e.preventDefault(); | |
var target = this.hash; | |
var $target = $(target); | |
$('html, body').stop().animate({ | |
'scrollTop': $target.offset().top | |
}, 900, 'swing', function () { | |
window.location.hash = target; | |
}); | |
}); | |
}); |
THANK YOU! This is the only code that's worked for me to keep the #hash in the link after smooth scroll.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you click on a link including a #hash this script checks if hash is on site and scrolls in this case. Otherwise it jumps to the url.