How to get scroll position of a pdf in an iframe?

Member

by shyann , in category: Javascript , 4 months ago

How to get scroll position of a pdf in an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 4 months ago

@shyann 

To get the scroll position of a PDF in an iframe, you can use JavaScript to access the iframe and get the scroll position of its content. Here's a step-by-step guide to achieving this:

  1. Access the iframe element in your HTML document using JavaScript:
1
const iframe = document.getElementById('your-iframe-id');


  1. Get the content window of the iframe:
1
const doc = iframe.contentDocument || iframe.contentWindow.document;


  1. Add an event listener to detect scroll events on the iframe content:
1
2
3
4
5
6
7
8
doc.addEventListener('scroll', function() {
   // Get the scroll position of the iframe content
   const scrollPosition = {
       x: doc.documentElement.scrollLeft,
       y: doc.documentElement.scrollTop
   };
   console.log(scrollPosition);
});


  1. When the iframe content is scrolled, the scroll event listener will log the scroll position in the console.


Make sure to replace 'your-iframe-id' with the actual ID of your iframe element in the code above. This code snippet will allow you to get the scroll position of a PDF document loaded in an iframe.