两个frame同步滚动

本文提供了一个简单的帧同步滚动Demo,通过JavaScript实现两个iframe内的滚动条同步滚动效果,并提供了开启和关闭同步的功能。
应网友要求,写了一个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>

 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值