LoginSignup
1
1

More than 1 year has passed since last update.

2つのiframeを同期スクロールさせる

Posted at
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <iframe id="iframeTxt1" src="longlong.txt"></iframe>
        <iframe id="iframeTxt2" src="longlong.txt"></iframe>
    <script>
        var iframe1 = document.getElementById("iframeTxt1");
        var iframe2 = document.getElementById("iframeTxt2");

        function scroll1() {
            iframe2.contentWindow.scrollTo(0,iframe1.contentWindow.pageYOffset);
        }
        function scroll2() {
            iframe1.contentWindow.scrollTo(0,iframe2.contentWindow.pageYOffset);
        }

        document.addEventListener('DOMContentLoaded', function() {
                iframe1.addEventListener('load', (function(element) {
                        return function() {
                                element.contentWindow.addEventListener('scroll', scroll1);
                        };
                })(iframe1, false));
                iframe2.addEventListener('load', (function(element) {
                        return function() {
                                element.contentWindow.addEventListener('scroll', scroll2);
                        };
                })(iframe2, false));
        }, false);
    </script>
    </body>
</html>

参考
https://qiita.com/sinsia/items/54865025821cb9203cfc

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1