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);
};
});
'jQuery' 카테고리의 다른 글
[jQuery] 현재 스크롤값 가져오기(scroll) (0) | 2019.05.01 |
---|---|
[jQuery] $(document).ready() vs window.onload (0) | 2019.04.29 |
[jQuery] lang() Selector (0) | 2019.04.27 |
[javascript] 동적으로 웹페이지 타이틀 변경 (0) | 2019.01.26 |
[jQuery] 접속 국가 ip check (0) | 2018.12.23 |