revision:
The scrollBy() method of the Element interface scrolls an element by the given amount.
scrollBy(x-coord, y-coord)
scrollBy(options)
Parameters:
x-coord : the horizontal pixel value that you want to scroll by.
y-coord : the vertical pixel value that you want to scroll by.
options : A dictionary containing the following parameters:
top : specifies the number of pixels along the Y axis to scroll the window or element.
left : specifies the number of pixels along the X axis to scroll the window or element.
behavior : determines whether scrolling is instant or animates smoothly. This option is a string which must take one of the following values:
smooth: scrolling should animate smoothly.
instant: scrolling should happen instantly in a single jump.
auto : scroll behavior is determined by the computed value of scroll-behavior.
// scroll an element
element.scrollBy(300, 300);
element.scrollBy({
top: 100,
left: 100,
behavior: "smooth",
});
Click one of the buttons (multiple times) to scroll the document.
Look at the scrollbar to see the effect.
<div>
<p>Click one of the buttons (multiple times) to scroll the document.</p>
<p>Look at the scrollbar to see the effect.</p>
<button onclick="scrollWin(0, 50)">Scroll down</button><br><br>
<button onclick="scrollWin(0, -50)">Scroll up</button><br><br>
</div>
<style>
body {height: 2100px;}
button { position: relative;}
</style>
<script>
function scrollWin(x, y) {
window.scrollBy(x, y);
}
</script>
Click one of the buttons (multiple times) to scroll the document.
Look at the scrollbar to see the effect.
<div>
<p>Click one of the buttons (multiple times) to scroll the document.</p>
<p>Look at the scrollbar to see the effect.</p>
<button onclick="scrollWin1(100, 0)">Scroll right</button><br><br>
<button onclick="scrollWin1(-100, 0)">Scroll left</button><br><br>
</div>
<style>
body {width: 1600px;}
button { position: relative;}
</style>
<script>
function scrollWin1(x, y) {
window.scrollBy(x, y);
}
</script>