How to scroll to top of the iframe?

by wilmer.lemke , in category: HTML & CSS , 3 months ago

How to scroll to top of the iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 3 months ago

@wilmer.lemke 

To scroll to the top of an iframe, you can use the scrollIntoView() method in JavaScript. Here's how you can do it:

  1. Get a reference to the iframe element using document.querySelector() or any other method.
  2. Call the scrollIntoView() method on the iframe element with the behavior option set to "smooth" to make the scrolling smooth.


Here's an example code snippet:

1
2
3
4
const iframe = document.querySelector('iframe');

// Scroll to the top of the iframe
iframe.scrollIntoView({ behavior: 'smooth', block: 'start' });


This code will scroll the viewport to the top of the iframe with a smooth animation. You can adjust the behavior option to suit your needs, such as instant scrolling or scrolling to the center of the iframe.