JQuery:如何同步滚动条

时间:2011-10-26 13:08:58

标签: javascript jquery iframe

如果我使用两个帧或iframe,如何同步两个水平滚动条?谢谢!

<SCRIPT language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></SCRIPT>

$(document).ready(function()
{
$("#div1").scroll(function () { 
    $("#div2").scrollTop($("#div1").scrollTop());
    $("#div2").scrollLeft($("#div1").scrollLeft());
});
$("#div2").scroll(function () { 
    $("#div1").scrollTop($("#div2").scrollTop());
    $("#div1").scrollLeft($("#div2").scrollLeft());
});

});

            <iframe id="div1" SRC="UserQueue_Header.asp?Occasion=<%=Occasion%>&GPL=<%=GPL%>" NAME="UserQueue_Header" width=100%>
                <p>Your browser does not support iframes.</p>
            </iframe>

            <iframe id="div2" SRC="UserQueue_Detail.asp?Occasion=<%=Occasion%>&GPL=<%=GPL%>" NAME="UserQueue_Detail" width=100%>
            </iframe>

3 个答案:

答案 0 :(得分:1)

代码没有收听iframe中内容的滚动事件。选择器需要看起来像

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>
//changed id since div is bad for iframes. :)
$(window).load( function(){
    var ifr1 = $( $('#iframe1').contents() );
    var ifr2 = $( $('#iframe2').contents() );

    ifr1.scroll( function(){ console.log("scroll");
         ifr2.scrollTop(ifr1.scrollTop());
         ifr2.scrollLeft(ifr1.scrollLeft());
    });

    ifr2.scroll( function(){
         ifr1.scrollTop(ifr2.scrollTop());
         ifr1.scrollLeft(ifr2.scrollLeft());
    });
});
</script>


<title>Test</title>
</head>
<body>
  <iframe src="test.html" id="iframe1"></iframe>
  <iframe src="test.html" id="iframe2"></iframe>
</body>
</html>

答案 1 :(得分:1)

这在IE和Firefox中都适用于我!

    <SCRIPT language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></SCRIPT>

$(window).load( function(){

  var ifr1 = $(window.frames[0]);
  var ifr2 = $(window.frames[1]);

  ifr1.scroll( function(){ 

     ifr2.scrollLeft(ifr1.scrollLeft());
  });

  ifr2.scroll( function(){

     ifr1.scrollLeft(ifr2.scrollLeft());
  });
});

<frameset>
    <frame src="test.html"/>
    <frame src="test.html"/>
</frameset>

答案 2 :(得分:0)

这适用于我在Firefox中,但不适用于IE。

<SCRIPT language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></SCRIPT>
<SCRIPT language="javascript" type="text/javascript" src="jquery.frameready.js"></SCRIPT>

$(document).ready(function()

{
// Sync Header Frame with Detail Frame`
$.frameReady( function() {

    var ifr2 = $(window.parent.document.getElementsByName('UserQueue_Detail')).contents();

   $(document).scroll( function(){

         ifr2.scrollLeft($(document).scrollLeft());
    });
  },
  "UserQueue_Header"
);

// Sync Detail Frame with Header Frame
$.frameReady( function() {

    var ifr1 = $(window.parent.document.getElementsByName('UserQueue_Header')).contents();

   $(document).scroll( function(){

         ifr1.scrollLeft($(document).scrollLeft());
    });
  },
  "UserQueue_Detail"
);

});