3

Possible Duplicate:
How to synchronize scrolling positions for several iframes

I have one HTML page containing two iframes. I want to synchronize the scroll bars of both the iframes. Meaning, if someone moves the horizontal or vertical scroll bar of one iframe then simultaneously the horizontal or vertical scroll bar of the other iframe should move in sync.

Community
  • 1
  • 1
ritesh1231
  • 31
  • 1
  • 2
  • You should have searched, there exists on Stack this question: http://stackoverflow.com/questions/762274/how-to-synchronize-scrolling-positions-for-several-iframes Googling your question would also have yielded many results. – Winfield Trail Aug 03 '11 at 16:35

1 Answers1

3

Here are some pointers:

  • You can access the iframe's window using the contentWindow attribute.

  • Register a scroll handler, contentWindow.onscroll, and you can scroll using scrollTo.

Security note: the content of the iframe's should be from the same domain as the top-level page, before it can access contentWindow's values, because cross-domain Javascript access is blocked.

frame1.contentWindow.onscroll = function(e) {
    frame2.contentWindow.scrollTop = frame1.contentWindow.scrollTop;
    frame2.contentWindow.scrollLeft = frame1.contentWindow.scrollLeft;
};
Community
  • 1
  • 1
Pindatjuh
  • 10,550
  • 1
  • 41
  • 68