2010-12-29 7 views
2

는 내가 현재 스크롤 POS를 얻을 내가 그것을 사용할 수 있도록 변수에 저장하는 방법을 잘 모르겠어요이jQuery를 저장 스크롤 위치

$('.button1').click(function() { 
//grab current scroll position 
    var currentscrollpos = $(window).scrollTop() 

    $("html, body").animate({ scrollTop: 0 }, 500); 

}); 

$('.button2').click(function() { 
    //go back to scroll position 
    $("html, body").animate({ scrollTop: currentscrollpos }, 500); 

}); 

처럼 클릭 문장의 몇 가지있어 내 다른 클릭 기능

방법이 있습니까?

답변

6

는 다른 기능으로 사용할 수 있도록 외부 범위의 변수를 정의 : 당신은 불필요한 변수와 네임 스페이스를 오염 방지를 위해 폐쇄에이 포장해야 할 수

var currentscrollpos; 

$('.button1').click(function() { 
    currentscrollpos = $(window).scrollTop() 
    $("html, body").animate({ scrollTop: 0 }, 500); 
}); 

$('.button2').click(function() { 
    $("html, body").animate({ scrollTop: currentscrollpos }, 500); 
}); 

그러나 이것은 당신이 시작 얻어야한다 적어도.