Sometimes, we want to scroll an iframe with JavaScript
In this article, we’ll look at how to scroll an iframe with JavaScript.
How to scroll an iframe with JavaScript?
To scroll an iframe with JavaScript, we can use the scrollTo
method.
For instance, we write
const myIframe = document.getElementById("iframe");
myIframe.onload = () => {
myIframe.contentWindow.scrollTo(xcoord, ycoord);
};
to get the iframe with getElementById
.
Then we set its onload
property to a function that scrolls itself to the x-y coordinates xcoord
and ycoord
with scrollTo
.
We get the iframe window with myIframe.contentWindow
.
Then we call scrollTo
on the window.
Conclusion
To scroll an iframe with JavaScript, we can use the scrollTo
method.