JavaScript window.scrollBy() Method
You are Here:
JavaScript window.scrollBy() Method
The window.scrollBy()
method scrolls the document in the window by the given amount.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">ScrollBy</button>
<script>
function myFunction(){
window.scrollBy(1000, 1000);
}
</script>
</body>
</html>
Syntax
window.scrollBy(x-coord, y-coord);
//or
window.scrollBy(option);
Parameter Values
Value | Type | Explanation |
---|---|---|
x-coord | Required | The pixel along the horizontal axis of the document that you want displayed in the upper left. |
y-coord | Required | The pixel along the vertical axis of the document that you want displayed in the upper left. |
scrollBy() with behavior
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">ScrollBy</button>
<script>
function myFunction(){
window.scrollBy({
top: 1000,
left: 1000,
behavior: "smooth"
});
}
</script>
</body>
</html>
scrollBy() for Specific Element
Note: You can also use scrollBy()
for specific element.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<div id="myDiv">
<p>Hello world.</p>
</div>
<button onclick="myFunction()">ScrollBy</button>
<script>
var x = document.getElementById("myDiv");
function myFunction(){
x.scrollBy({
top: 100,
left: 100,
behavior: 'smooth'
});
}
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.