How to bind multiple elements to scroll together?

Barrett Kuethen

I have 3 different tables that scroll horizontally (one scrolls vertically). How do I bind them all together so if I scroll horizontally on one, they all scroll?

I've read through here (Mozilla docs) but I can't get it to work.

It seems like it should be fairly simple.

<div class="page-wrapper relative">
<div class="table-wrapper-outter">
    <div class="table-wrapper">
        <div class="header header-wrapper">
            <div class="header-inner">
                <table id="headerTable" class="table-contents">
                    <tr>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> etc... </td>
                    </tr>

                </table>
            </div>
        </div>
        <div id="bodyHolder" class="table-content-outter">
            <div class="table-content-inner">
                <table id="mainTable" class="table-contents">
                        <tr>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> 1234567890 </td>
                            <td> etc... </td>
                        </tr>
                        <!-- many rows -->

                </table>
            </div>
        </div>
        <div class="header footer-wrapper">
            <div class="header-inner">
                <table id="footerTable" class="table-contents">
                    <tr>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> 1234567890 </td>
                        <td> etc... </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

And here is the javascript I tried:

function megaTableInit(tableBodyHolder, mainTable, headerTable, footerTable, tablePullDivs, tableRowHeads) {
var bodyHolder = document.getElementById(tableBodyHolder);
var mTable = document.getElementById(mainTable);
var hTable = document.getElementById(headerTable);
var fTable = document.getElementById(footerTable);
var pullDivs = document.getElementsByClassName(tablePullDivs);
var rowHeads = document.getElementsByClassName(tableRowHeads);

bodyHolder.onmousemove = bodyHolder.onscroll = function() {

    pullDivs[0].scrollLeft = this.scrollLeft;

    var sl = this.scrollLeft;

    if(this.oldSL != sl) {
        this.oldSL = sl;

        for (i=0; i<pullDivs.length; i++) {
            pullDivs[i].scrollLeft = this.scrollLeft;
        }
    }
}

I don't get any errors. It just doesn't do anything with the over divs. When I run console.log for sl and this.scrollLeft I get the pixels as I scroll. But it doesn't set the scroll for the other divs.

Any help would be appreciated.

Ricky Nelson

I'm not sure if this is exactly what you want but it does what you described, it scrolls all the others when you scroll 1.

$('.scroller').scroll(function(e){
  $('.scroller').scrollLeft(e.target.scrollLeft);
});

http://codepen.io/anon/pen/QbaKNz

EDIT: here is the same example with vanilla JavaScript:

var scrollers = document.getElementsByClassName('scroller');

var scrollerDivs = Array.prototype.filter.call(scrollers, function(testElement) {
  return testElement.nodeName === 'DIV';
});

function scrollAll(scrollLeft) {
  scrollerDivs.forEach(function(element, index, array) {
    element.scrollLeft = scrollLeft;
  });
}

scrollerDivs.forEach(function(element, index, array) {
  element.addEventListener('scroll', function(e) {
    scrollAll(e.target.scrollLeft);
  });
});

http://codepen.io/anon/pen/zGpNrm

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add multiple lists together, while keeping them as elements in Python?

分類Dev

How to bind icon and text together?

分類Dev

How to bind two ng-models values together

分類Dev

How to bind two ng-models values together

分類Dev

How do I bind the function and button together in reactjs

分類Dev

How to bind multiple tables to single DM form?

分類Dev

Three.js - How to group together multiple imported models

分類Dev

How to wait for multiple observable streams to resolve and return them together

分類Dev

How to pass multiple values of type NSString together for a query?

分類Dev

How to lookup multiple items add their corresponding values together and generate a percentage

分類Dev

Bind window scroll the AngularJS way

分類Dev

Bind vertical scroll position to counter

分類Dev

FadeIn elements on scroll

分類Dev

Bring equal list elements together

分類Dev

How to create single object with multiple keys and elements?

分類Dev

How to apply a CSS property to multiple elements individualy

分類Dev

How to format a nested multiple-value-bind the let way?

分類Dev

AngularJS: bind scroll event to one controller not all

分類Dev

.bind('scroll'), add animation or reduce "bounce"

分類Dev

Bind to multiple Properties in UWP

分類Dev

Bind different dom elements in Angular

分類Dev

How to run multiple .js test files together using npm (pact files)?

分類Dev

How to add a file name to a column in a data frame as multiple files are merged together?

分類Dev

Django - how to combine multiple filter parameters together, where the number of parameters is variable

分類Dev

How to add multiple dictionaries together from a dictionary generator to create single collated python dictionary

分類Dev

Using pandas to add list elements together

分類Dev

Efficiently multiply elements of each row together

分類Dev

combine elements of a list together forming new lists

分類Dev

how to sort a set when elements have multiple relationships with each other

Related 関連記事

  1. 1

    How to add multiple lists together, while keeping them as elements in Python?

  2. 2

    How to bind icon and text together?

  3. 3

    How to bind two ng-models values together

  4. 4

    How to bind two ng-models values together

  5. 5

    How do I bind the function and button together in reactjs

  6. 6

    How to bind multiple tables to single DM form?

  7. 7

    Three.js - How to group together multiple imported models

  8. 8

    How to wait for multiple observable streams to resolve and return them together

  9. 9

    How to pass multiple values of type NSString together for a query?

  10. 10

    How to lookup multiple items add their corresponding values together and generate a percentage

  11. 11

    Bind window scroll the AngularJS way

  12. 12

    Bind vertical scroll position to counter

  13. 13

    FadeIn elements on scroll

  14. 14

    Bring equal list elements together

  15. 15

    How to create single object with multiple keys and elements?

  16. 16

    How to apply a CSS property to multiple elements individualy

  17. 17

    How to format a nested multiple-value-bind the let way?

  18. 18

    AngularJS: bind scroll event to one controller not all

  19. 19

    .bind('scroll'), add animation or reduce "bounce"

  20. 20

    Bind to multiple Properties in UWP

  21. 21

    Bind different dom elements in Angular

  22. 22

    How to run multiple .js test files together using npm (pact files)?

  23. 23

    How to add a file name to a column in a data frame as multiple files are merged together?

  24. 24

    Django - how to combine multiple filter parameters together, where the number of parameters is variable

  25. 25

    How to add multiple dictionaries together from a dictionary generator to create single collated python dictionary

  26. 26

    Using pandas to add list elements together

  27. 27

    Efficiently multiply elements of each row together

  28. 28

    combine elements of a list together forming new lists

  29. 29

    how to sort a set when elements have multiple relationships with each other

ホットタグ

アーカイブ