답변:
다음 window.scrollTo()과 같이 사용할 수 있습니다 .
window.scrollTo(0, 0); // values are x,y-offset
전체 창 대신 요소를 스크롤하려는 경우 요소에는 scrollTo및 scrollBy메서드 가 없습니다 . 다음을 수행해야합니다.
var el = document.getElementById("myel"); // Or whatever method to get the element
// To set the scroll
el.scrollTop = 0;
el.scrollLeft = 0;
// To increment the scroll
el.scrollTop += 100;
el.scrollLeft += 100;
기본적으로 지원하지 않는 브라우저 에서 웹 페이지의 모든 기존 HTML 요소에 window.scrollTo및 window.scrollBy함수를 모방 할 수도 있습니다 .
Object.defineProperty(HTMLElement.prototype, "scrollTo", {
value: function(x, y) {
el.scrollTop = y;
el.scrollLeft = x;
},
enumerable: false
});
Object.defineProperty(HTMLElement.prototype, "scrollBy", {
value: function(x, y) {
el.scrollTop += y;
el.scrollLeft += x;
},
enumerable: false
});
그래서 당신은 할 수 있습니다 :
var el = document.getElementById("myel"); // Or whatever method to get the element, again
// To set the scroll
el.scrollTo(0, 0);
// To increment the scroll
el.scrollBy(100, 100);
참고 : Object.defineProperty에 속성을 직접 추가하는 prototype것은 나쁜 습관을 깨는 것이므로 권장됩니다 (보면 :-).