2015-01-13 5 views
0

이 스크립트를 내 프로젝트에서 사용하기 위해 하나의 스크롤 페이지가 있습니다. 이제 내 문제는 제목이 내 고정 메뉴에 항상 숨겨져있는 정확한 섹션에서 페이지가 스크롤 될 때마다 발생했습니다. 아래는 스크립트입니다이 스크립트에 클래스를 추가하려면 어떻게해야합니까?

$('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 
      }, 1500,'easeInOutExpo'); 
      return false; 
     } 
    } 
}); 

당신이 도와 줄 수 있기를 바랍니다. 많은 감사!

+0

에 오신 것을 환영합니다. 귀하의 질문에 올바르게 대답하기 위해서는 html이 필요합니다. 당신은 당신의 문제를 shwowing 바이올린이나 webadress를 제공 할 수 있습니까? –

+1

'scrollTop'을 적용 할 때 고정 헤더의 높이를 고려해야합니다. 'scrollTop : target.offset(). top + $ ('header')와 같은 것. height()' –

+0

@IanBrindley 빠른 응답에 감사드립니다. 내 문제를 해결했다. – anzaimurakami

답변

1
내 의견이 충분히 명확 wan't 경우 scrollTop

을 적용 할 때 당신은 계정에 고정 된 헤더의 높이를 수행해야합니다

, 이것을 시도.

$('a[href*=#]:not([href=#])').click(function() { 
// does the pathname of this link match that of the current location.pathname? 
var activeLink = (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')); 

if (activeLink || location.hostname == this.hostname) { 

    var target = $(this.hash); 

    target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 

    if (target.length) { 

     // here we will take into account the height of #navbar 
     var offset = target.offset().top + $('#navbar').height(); 

     $('html,body').animate({ 
      scrollTop: offset // apply our new offset 
     }, 1500,'easeInOutExpo'); 

     return false; 
    } 
} 

});

관련 문제