2013-01-09 4 views
0

아래에 표시된 jquery 스크롤 div를 사용하고 있습니다.하지만 처음 링크를 클릭하면 스크롤되어 맨 위의 오프셋이 무시되지만 링크를 다시 클릭하면 자신을 바로 잡을 것입니다. JQuery div에 스크롤 오프셋을 무시합니다

$(document).ready(function() { 
    function filterPath(string) { 
    return string 
    .replace(/^\//,'') 
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'') 
    .replace(/\/$/,''); 
    } 
    var locationPath = filterPath(location.pathname); 
    var scrollElem = scrollableElement('html', 'body'); 

    $('a[href*=#]').each(function() { 
    var thisPath = filterPath(this.pathname) || locationPath; 
    if ( locationPath == thisPath 
    && (location.hostname == this.hostname || !this.hostname) 
    && this.hash.replace(/#/,'')) { 
     var $target = $(this.hash), target = this.hash; 
     if (target) { 
     var targetOffset = $target.offset().top -100; 
     $(this).click(function(event) { 
      event.preventDefault(); 
      $(scrollElem).animate({scrollTop: targetOffset}, 400, function() { 
      location.hash = target; 
      }); 
     }); 
     } 
    } 
    }); 

    function scrollableElement(els) { 
    for (var i = 0, argLength = arguments.length; i <argLength; i++) { 
     var el = arguments[i], 
      $scrollElement = $(el); 
     if ($scrollElement.scrollTop()> 0) { 
     return el; 
     } else { 
     $scrollElement.scrollTop(1); 
     var isScrollable = $scrollElement.scrollTop()> 0; 
     $scrollElement.scrollTop(0); 
     if (isScrollable) { 
      return el; 
     } 
     } 
    } 
    return []; 
    } 

}); 

는 처음에 나는 그것이 문제가 내 로컬 컴퓨터 떨어져 그것을 실행되었을 수 있습니다 생각하지만 페이지가 서버에 업로드 할 때이 오류가 발생합니다.

도움을 주시면 감사하겠습니다.

+0

'location.hash = target;'을 (를) 제거하면 어떻게됩니까? – Blazemonger

+0

@Blazemonger 그게 고쳐 주셔서 고마워요 !! :) –

답변

0

.click 이벤트 핸들러에서 location.hash = target;을 삭제 해보세요. 애니메이션이 완료되면 점프가 발생합니다.