[javascript] jQuery에서 창의 높이와 스크롤 위치를 어떻게 결정합니까?

jQuery에서 창의 높이와 스크롤 오프셋을 가져와야하지만 jQuery 문서 또는 Google에서 이것을 찾지 못했습니다.

90 %는 요소 (아마도 창 포함)의 높이와 scrollTop에 액세스 할 수있는 방법이 있지만 특정 참조를 찾을 수는 없습니다.



답변

jQuery 문서에서 :

const height = $(window).height();
const scrollTop = $(window).scrollTop();

http://api.jquery.com/scrollTop/
http://api.jquery.com/height/


답변

http://api.jquery.com/height/ (참고 : 윈도우의 이용 및 문서 개체 차이)

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

에서 http://api.jquery.com/scrollTop/

$(window).scrollTop() // return the number of pixels scrolled vertically


답변

순수한 JS

window.innerHeight
window.scrollY

jquery보다 10 배 이상 빠릅니다 (코드의 크기는 비슷합니다).

여기에 이미지 설명을 입력하십시오

다음은 컴퓨터에서 테스트를 수행 할 수 있습니다 : https://jsperf.com/window-height-width


답변

$(window).height()

$(window).width()

요소 위치와 오프셋을 결정하기 위해 jquery에 플러그인이 있습니다.

http://plugins.jquery.com/project/dimensions

스크롤 오프셋 = 요소의 offsetHeight 속성


답변

요소의 지점으로 스크롤해야하는 경우 Jquery 기능을 사용하여 위 / 아래로 스크롤 할 수 있습니다.

$('html, body').animate({
                scrollTop: $("#div1").offset().top
            }, 'slow');


답변