2013-08-07 3 views
12

페이지가로드 될 때 페이지가 특정 div로 자동 스크롤되도록하는 방법을 파악하려고합니다. 나는 jQuery 스크롤 함수를 사용해 보았지만 제대로 작동하지 않는다. 제안 사항이 있으십니까?페이지로드시 특정 div로 스크롤

jQuery(function() { 
jQuery(window).scrollTop(jQuery('.container').offset().top); 
}); 

답변

28

당신은 .animate() 방법을 사용하여이 작업을 수행 할 수 있습니다 : 다음

내가 지금까지 시도한 것입니다 이것은 ID로 DIV로 스크롤을 부드럽게합니다

$(document).ready(function() { 
    // Handler for .ready() called. 
    $('html, body').animate({ 
     scrollTop: $('#what').offset().top 
    }, 'slow'); 
}); 
  • what

FIDDLE

+1

감사합니다. 보드가 나를 보냈을 때 당신의 대답을 받아 들일 것입니다. – simon

+1

다행히 도왔습니다 :) –

+0

이것이 동적으로 수행 될 수 있습니까? 다음과 같은 쿼리 문자열에 div 이름을 보내고 싶습니다. show.aspx? divToScrollTo = SecondDiv. – John

0
$(document).ready(function(){ 
    $("html, body").animate({ 
     scrollTop: $('.sb-menu').offset().top 
    }, 1000); 
}); 
0

최초 당신은 여기

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 

ID가 '스크롤'입니다 파일을 호출해야합니다. 다음 코드가 도움이됩니다.

<html> 
<head> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script type="text/javascript"> 

$(document).ready(function() { 
    // Handler for .ready() called. 
    $('html, body').animate({ 
     scrollTop: $('#scroll').offset().top 
    }, 'slow'); 
}); 

</script> 

</head> 
<body> 

<div id="scroll"></div> 

</body> 
</html> 
관련 문제