2012-01-30 4 views
2

안녕하세요.jQuery를 사용하여 위치로 스크롤

이 버튼을 클릭하면 자동으로 아래로 스크롤하려고합니다.

scrollIndex라는 변수에 내 버튼의 y- 인덱스 값이 저장되어 있습니다 (offset() 메소드 사용).

그리고 이제 나는이 내 창을 아래로 스크롤하려고 해요 : 나는 실제로 많은 것들을 시도했지만 아무것도 작동하는 것 같다없고, 내가 잘못을 만들고있어 그것 때문에 supose

var scrollIndex = $('#tourbutton').offset(); 
$("body").animate({scrollTop:(scrollIndex.top)},500,"linear"); 

scrollTop 메서드() 사용 ..

어떤 도움이 필요합니까? 고맙습니다!

내 전체 코드 :

window.onload = initAll; 

function initAll() { 
    changeStyle(); 
    $('#tourbutton').click = hello(); 
} 

function hello() { 
    var scrollIndex = $('#tourbutton').offset(); 
    $("html, body").animate({scrollTop:(scrollIndex.top)},800,"swing"); 
    //var scrollIndex = $('#tourbutton').offset();  
    //$("window").scrollTo({top:scrollIndex},800); 
} 

해결! 난 그냥이 같은 클릭 (내부의 모든) 작성 :

function initAll() { 
$('#tourbutton').click(function() { 

var scrollIndex = $('#tourbutton').offset(); 
$("html, body").animate({scrollTop:(scrollIndex.top)},800,"swing"); 

//var scrollIndex = $('#tourbutton').offset(); 
//$("window").scrollTo({top:scrollIndex},800); 
}); 

    changeStyle(); 
} 

을 그리고 모든 사람이 이해하는 경우에 나는거야 외부 함수에 클릭 이벤트 핸들러를 연결할 수없는 이유를 확실히 ... 왜 이해가 안 나는 제외하고 지금 작동 답을 알고 기뻐하십시오.

모두에게 감사드립니다!

+0

시도'$ ('문서'() {scrollTop : (scrollIndex.top)}, 500, "선형") 애니메이션;. '를? –

+0

[jQuery.ScrollTo] (http://demos.flesler.com/jquery/scrollTo/) – TOUDIdel

+0

흠 나는 이것을 썼다 : var scrollIndex = $ ('# tourbutton'). offset(); \t $ ("window"). scrollTo ({top : scrollIndex}, 800); 그리고 나는 "$ ("window ") 오류가 발생합니다. scrollTo는 함수가 아닙니다." –

답변

5

Chrome에서 테스트 중입니까? 크롬에서 100 % 몸체 높이를 지정한 다음 스크롤 위치를 설정하면 작동하지 않는 다른 시점에서 다른 질문이 발생했습니다. 스크롤 할 수있는 div에 사이트 본문을 넣거나 100 % 높이를 지정하지 않아야합니다.

이 시도 :

var scrollIndex = $('#tourbutton').offset(); 
$("html, body").animate({scrollTop:(scrollIndex.top)},500,"linear"); 
+0

나는 당신이 말한 것을했는데 지금은 효과가 있지만 내 페이지를로드 할 때마다 그것을한다! 내 전체 코드는 다음과 같습니다 : –

+0

window.onload = initAll; 함수 initAll() { \t changeStyle(); \t $ ('# tourbutton'). click = hello(); } function hello (\t \t var scrollIndex = $ ('# tourbutton'). offset(); \t $ ("html, body"). animate ({scrollTop : (scrollIndex.top)}, 800, "swing"); \t // var scrollIndex = $ ('# tourbutton'). offset(); \t //$("window").scrollTo({top:scrollIndex},800); } –

관련 문제