jQuery
[jQuery] How to smooth scroll to an id after clicking a link from another page
cpu21
2019. 4. 27. 19:26
When using $('a[href*=\\#]'), it only/automatically applies to all anchors (<a>) with a hash (#).
$(document).ready(function() {
$('a[href*=\\#]').on('click', function(e){
e.preventDefault();
$('html, body').animate({
scrollTop : $(this.hash).offset().top
}, 500);
});
});
Update
In case you want to smooth scroll to an element on a new page, include the following code on that page:
$(document).ready(function() {
if (window.location.hash) {
var hash = window.location.hash;
$('html, body').animate({
scrollTop : $(hash).offset().top
}, 500);
};
});