JavaScript에서 요소의 콘텐츠가 수직으로 스크롤 된 픽셀 수를 설정하거나 반환하는 방법은 무엇인가요?


JavaScript에서는 scrollTop 속성을 사용하여 요소의 콘텐츠가 수직으로 스크롤 된 픽셀 수를 설정하거나 반환할 수 있습니다. 이 속성은 읽기/쓰기 모두 가능합니다.

예를 들어, element.scrollTop를 사용하여 요소의 스크롤 위치를 가져올 수 있습니다.

const element = document.getElementById("myElement");
const scrollTopValue = element.scrollTop;
console.log(scrollTopValue); // 출력: 스크롤 된 픽셀 수

또는 element.scrollTop = value를 사용하여 요소의 스크롤 위치를 설정할 수 있습니다.

const element = document.getElementById("myElement");
element.scrollTop = 100; // 요소의 스크롤 위치를 100px로 설정


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.