How can I make 2 div's scroll at the same time

10,728

Here is an example that positions the bottom div dynamically behind the top div and as you scroll the top div, the bottom div dynamically scrolls.

<html>
    <head>
        <title>Test</title>
        <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
        <script>
            $(document).ready(function(){
                var topdivPos = $("#topdiv").position();
                $("#bottomDiv").css("position","absolute").css("top",topdivPos.top).css("left",topdivPos.left).show();
                $("#topdiv").scroll(function(){
                    $("#bottomDiv").scrollTop($("#topdiv").scrollTop());
                });
            });
        </script>
    </head>
    <body>
        <div id="topdiv" style="width:300; height:100; overflow:auto">
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
        </div>
        <div id="bottomDiv" style="display:none;width:300; height:100; overflow:auto;z-index:-100">
            <img src="http://www.pixtual.com/wallpaper/Pixtual-10-3-2008.png"/>
        </div>
    </body>
</html>
Share:
10,728
desbest
Author by

desbest

SKILLS Algolia APIs C C++ Chat rooms (long polling &amp; websockets) Codeigniter CSS Facebook API Gun DB HTML Importing large MySQL/MariaDB databases from a file Javascript jQuery Laravel Magento PHP PSD to HTML Processing Processwire Ruby Scraping Symfony Websockets Wordpress Technologies I will not work with Ruby on Rails (rails)

Updated on June 04, 2022

Comments

  • desbest
    desbest over 1 year

    For my website design, I have a <div class="stuffhere"></div> where the content is stored. There are 2 of these div's. The second one is in front of the first one, with the help of z-index: Also the content in both stuffhere tags are duplicated, but sometimes the images in the top layer stuffhere is replaced with spacers.

    I can describe the reason for this setup best with an image.

    Update: (new images!) how my code works how my code renders the page http://img5.imageshack.us/img5/8246/portfolio4.png

    Notice how the Casey web design image is not affected by the stripy background, whereas the other blocks are. The Casey web design image is the only element on the screenshot which is not tinted by the background.

    This is because elements inside the back div are tinted with the stripey background, whereas elements in the front div are not.

    The back div has an opacity of 0.55 with the stripy background. As the front div has no background and has an opacity of 1, its contents are not affected by the stripy background, hence it's not tinted. Yes it does tint. I can illustrate that with another image.

    my portfolio site tinted

    In the same code snapshot below, showing you it again, you will see that for some images overlap each other. A good example of this is the one called work_title_heroesforhire.png and work_title_heroesforhire-F.png how my code works

    Why is this?

    Below is the image without the F suffix. enter image description here It's affected by opacity as it's placed in the back layer.

    Below is the image with the F suffix. It's not affected by opacity as it's placed in the front layer. enter image description here

    This is why the text inside the screenshots does not have a tint of the stripy background.

    Now you understand how my design works, here's the problem. When I scroll, this happens... mmy portfolio site without sync'ed scrolling

    How can I make 2 div's scroll at the same time? Is there a way I can synchronise the scrolling of 2 div's with javascript? JQuery is preferable.

    Update Working example webpage you can look at

  • desbest
    desbest about 12 years
    I would like the position of the bottom div to be the in the same position as the top div, so the positions are synchronised. How can I do that?