frequently ask ? : Computers : Programming : Languages : JavaScript : Frames

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

76 of 81 people (94%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I synchronize the scrolling of two frames?

Jan 30th, 2001 13:28
Martin Honnen,


You can use
  setInterval
to periodically read the viewport coordinates of one frame and then 
scroll the other frame to that position with
  frameReference.scrollTo(x, y)
in NN4, NN6, Opera4/5 and IE4+.
In NN4 and Opera4/5 the code set up in the frameset document works even 
when the frames are on a different host, with NN6 and IE4+ the frameset 
needs to come from the same host as the frames.
<HTML>
<HEAD>
<TITLE>
frame scrolling synchronization
</TITLE>
<SCRIPT>
var tid;
function initScrollSynchronization () {
  tid = setInterval('syncFrame()', 25);
}
function syncFrame () {
  if (document.all && !window.opera) {
    var scrollTop = frame0.document.body.scrollTop;
    var scrollLeft = frame0.document.body.scrollLeft;
  }
  else {
    var scrollTop = frame0.pageYOffset;
    var scrollLeft = frame0.pageXOffset;
  }
  frame1.scrollTo (scrollLeft, scrollTop);
}
</SCRIPT>
</HEAD>
<FRAMESET COLS="50%, 50%" ONLOAD="initScrollSynchronization()">
<FRAME NAME="frame0" SRC="file1.html">
<FRAME NAME="frame1" SRC="file2.html">
</FRAMESET>
</HTML>