문서의 스크롤 위치 값을 얻는 방법은 무엇입니까?
답변:
$(document).height() //returns window height
$(document).scrollTop() //returns scroll position from top of document
scrollHeight.
다음 scrollHeight은 jQuery 선택기를 사용하여 얻은 요소 를 가져 오는 방법입니다 .
$(selector)[0].scrollHeight
경우 selector요소 (예를 들면의 ID입니다 elemId), 배열의 0 인덱스 항목을 선택하고자하는 요소가 될 것이며, 보장되는 scrollHeight올바른 것입니다.
Jquery 1.6 이상을 사용하는 경우 값에 액세스하려면 prop을 사용하십시오.
$(document).prop('scrollHeight')
이전 버전은 attr에서 값을 가져 오는 데 사용되었지만 1.6 이후에는 사용되지 않았습니다.
document.getElementById("elementID").scrollHeight
$("elementID").scrollHeight
이와 같은 방법으로 문제를 해결할 수 있습니다.
$.getDocHeight = function(){
var D = document;
return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};
alert( $.getDocHeight() );
추신 : 필요할 때마다 해당 함수를 호출하십시오. 경고는 테스트 목적입니다.
창 스크롤바로 스크롤되는 영역의 실제 스크롤 가능 높이를 얻으려면 $('body').prop('scrollHeight'). 이것은 가장 간단한 작업 솔루션 인 것처럼 보이지만 호환성을 광범위하게 확인하지 않았습니다. Emanuele Del Grande는 이것이 아마도 8 이하의 IE에서는 작동하지 않을 것이라고 다른 솔루션에 대해 언급합니다.
대부분의 다른 솔루션은 스크롤 가능한 요소에 대해 잘 작동하지만 이는 전체 창에서 작동합니다. 특히, 그, 즉, ANKIT의 솔루션을 마이클과 같은 문제가 있었다 $(document).prop('scrollHeight')반환합니다 undefined.