2012-08-12 2 views
1

을 상쇄하는 것은 내가 브라우저가 근처 에서 #menu의 DIV의 상단을 표시하고 싶은 링크의 클릭에 나는 다음과 같은 한 scrollTop

$('html,body').animate({scrollTop: $('#menu').offset().top}, 'slow'); 

스 니펫. 나는 그것이 메뉴 앞에 단지 몇 픽셀 라인을 표시하고 싶다.

어떻게하면됩니까?

offset()에 paddingTop : 5를 추가했지만 원하는 결과가 아닙니다.

+1

... 너는 너무 멀리 생각하고 있었다. 왜 단지 $ ('# menu')에 5를 더하지 않는가? offset(). top'? – Kiruse

답변

14

그냥 당신이 $('#menu').offset().top에서 원하는 금액 차감 :

$('html,body').animate({ 
    scrollTop: $('#menu').offset().top - 5 // or 10 
}, 'slow'); 

는 여기에 바이올린입니다 : http://jsfiddle.net/qVWuv/

0

이 코드를 수정했다.

$(function() { 
     $('a').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(0) +']'); 
      if (target.length) { 
      $('#content').animate({ 
       scrollTop: target.offset().top - 15 
      }, 1000); 
      return false; 
      } 
     } 
     }); 
    });