2016-06-17 15 views
0

다음 코드를 사용하여 내 사이트의 앵커에 대한 부드러운 스크롤을 추가합니다. 나는 끈적 끈적한 헤더 ID가이를 상쇄하기 좋아 가지고 있기 때문에 에 의해 200 픽셀오프셋이있는 부드러운 스크롤링 앵커 (jquery)

$('a[href*="#"]:not([href="#"])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
      $('html, body').animate({ 
       scrollTop: target.offset().top 
      }, 1000); 
      return false; 
     } 
    } 
}); 

답변

1

추가 시도하거나 scrollTop 애니메이션에 값을 제거 말을

$('a[href*="#"]:not([href="#"])').click(function() { 
    var offset = -200; // <-- change the value here 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
      $('html, body').animate({ 
       scrollTop: target.offset().top + offset 
      }, 1000); 
      return false; 
     } 
    } 
});