应网友要求,写了一个frame同步滚动的DEMO,很简单,收录在这里:
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
< title ></ title >
< style type ="text/css" >
body {
font-size:9pt;
color:#333333;
}
iframe {
position:absolute;
width:200px;
height:400px;
margin:20px;
border:1px solid #FF6600;
}
#ifr1 {
left:20px;
top:20px;
}
#ifr2 {
left:320px;
top:20px;
}
</ style >
< script type ="text/javascript" >
function FrameHelper(ifr1,ifr2){
this.ifr1 = ifr1;
this.ifr2 = ifr2;
this.syncable = true;
this.bindEvent();
}
FrameHelper.prototype = {
sync:function(activeFrame){
if(!this.syncable)return;
var passiveFrame = (activeFrame == this.ifr1) ? this.ifr2 : this.ifr1;
if(activeFrame.document.documentElement){
passiveFrame.document.documentElement.scrollTop = activeFrame.document.documentElement.scrollTop;
passiveFrame.document.documentElement.scrollLeft = activeFrame.document.documentElement.scrollLeft;
}else{
passiveFrame.document.body.scrollTop = activeFrame.document.body.scrollTop;
passiveFrame.document.body.scrollLeft = activeFrame.document.body.scrollLeft;
}
},
bindEvent:function(){
var _this = this;
if(document.all){
this.ifr1.attachEvent("onscroll",function(){_this.sync(_this.ifr1);});
this.ifr2.attachEvent("onscroll",function(){_this.sync(_this.ifr2);});
}else{
this.ifr1.addEventListener("scroll",function(){_this.sync(_this.ifr1);},false);
this.ifr2.addEventListener("scroll",function(){_this.sync(_this.ifr2);},false);
}
},
syncModeOn:function(){
this.syncable = true;
},
syncModeOff:function(){
this.syncable = false;
}
}
window.onload = function(){
var fh = new FrameHelper(document.getElementById("ifr1").contentWindow,document.getElementById("ifr2").contentWindow);
document.getElementById("toggle").onchange = function(){
(!!this.checked)?fh.syncModeOn():fh.syncModeOff();
}
}
</ script >
</ head >
< body >
< iframe id ="ifr1" src ="s.html" ></ iframe >
< iframe id ="ifr2" src ="s.html" ></ iframe >
< input type ="checkbox" id ="toggle" checked ="checked" /> 是否同步滚动
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
< title ></ title >
< style type ="text/css" >
body {
font-size:9pt;
color:#333333;
}
iframe {
position:absolute;
width:200px;
height:400px;
margin:20px;
border:1px solid #FF6600;
}
#ifr1 {
left:20px;
top:20px;
}
#ifr2 {
left:320px;
top:20px;
}
</ style >
< script type ="text/javascript" >
function FrameHelper(ifr1,ifr2){
this.ifr1 = ifr1;
this.ifr2 = ifr2;
this.syncable = true;
this.bindEvent();
}
FrameHelper.prototype = {
sync:function(activeFrame){
if(!this.syncable)return;
var passiveFrame = (activeFrame == this.ifr1) ? this.ifr2 : this.ifr1;
if(activeFrame.document.documentElement){
passiveFrame.document.documentElement.scrollTop = activeFrame.document.documentElement.scrollTop;
passiveFrame.document.documentElement.scrollLeft = activeFrame.document.documentElement.scrollLeft;
}else{
passiveFrame.document.body.scrollTop = activeFrame.document.body.scrollTop;
passiveFrame.document.body.scrollLeft = activeFrame.document.body.scrollLeft;
}
},
bindEvent:function(){
var _this = this;
if(document.all){
this.ifr1.attachEvent("onscroll",function(){_this.sync(_this.ifr1);});
this.ifr2.attachEvent("onscroll",function(){_this.sync(_this.ifr2);});
}else{
this.ifr1.addEventListener("scroll",function(){_this.sync(_this.ifr1);},false);
this.ifr2.addEventListener("scroll",function(){_this.sync(_this.ifr2);},false);
}
},
syncModeOn:function(){
this.syncable = true;
},
syncModeOff:function(){
this.syncable = false;
}
}
window.onload = function(){
var fh = new FrameHelper(document.getElementById("ifr1").contentWindow,document.getElementById("ifr2").contentWindow);
document.getElementById("toggle").onchange = function(){
(!!this.checked)?fh.syncModeOn():fh.syncModeOff();
}
}
</ script >
</ head >
< body >
< iframe id ="ifr1" src ="s.html" ></ iframe >
< iframe id ="ifr2" src ="s.html" ></ iframe >
< input type ="checkbox" id ="toggle" checked ="checked" /> 是否同步滚动
</ body >
</ html >