带有两个 div 的链接水平滚动



>我有两个表,一个是只有表头的表,另一个表包含所有表数据。两个表都在各自独立的div 内。我正在尝试使在表数据div 上水平滚动将触发 JavaScript 中的一个事件,该事件将以相同的速率滚动表头div。我知道我可以摆脱div,只有一个带有粘性标题的表,但我想尝试这样做。这是我认为可以工作的简化版本的代码:

.HTML:

<div id = "div1">
  <table id = "stickyheaders" class = "table table-condensed table-striped small">
    <thead><tr>
      <th>header1</th>
      <th>header2</th>
      <th>header3</th>
      <th>header4</th>
      <th>header5</th>
      <th>header6</th>
      <th>header7</th>
      <th>header8</th>
      <th>header9</th>
      <th>header10</th>
    </tr></thead>
  </table>
</div>
<div id = "div2">
  <table id = "tablebody" class = "table table-condensed table-striped small">
    <tbody>
      <tr>
        <td>data1</td>
        <td>data2</td>
        <td>data3</td>
        <td>data4</td>
        <td>data5</td>
        <td>data6</td>
        <td>data7</td>
        <td>data8</td>
        <td>data9</td>
        <td>data10</td>
      </tr>
    </tbody>
  </table>
</div>

JavaScript:

$(document).ready(function() {
    $('#div2').on('scroll', function () {
        $('#').scrollLeft($(this).scrollLeft());
    });
} )();

这是小提琴

我在这里错过了什么愚蠢的东西吗?提前感谢您的帮助。我知道这与这里提出的另一个问题相似,但这个问题没有答案,也没有真正帮助我。

你缺少核心滚动的东西。将$('#')更换为正确的id,并取下末尾的()。是的,添加jQuery:

$(document).ready(function() {
  $('#div2').on('scroll', function() {
    $('#div1').scrollLeft($(this).scrollLeft());
  });
});
#div1 {
  width: 50%;
  height: 40px;
  padding: 10px;
  border: 1px solid #c0c0c0;
  border-radius: 5px;
  overflow-y: auto;
  overflow-x: auto;
  label {
    display: block;
  }
  tr:after {
    content: ' ';
    display: block;
    visibility: auto;
    clear: both;
  }
}
#div2 {
  width: 50%;
  height: 50px;
  padding: 10px;
  border: 1px solid #c0c0c0;
  border-radius: 5px;
  overflow-y: auto;
  overflow-x: auto;
  label {
    display: block;
  }
  tr:after {
    content: ' ';
    display: block;
    visibility: auto;
    clear: both;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1">
  <table id="stickyheaders" class="table table-condensed table-striped small">
    <thead>
      <tr>
        <th>header1</th>
        <th>header2</th>
        <th>header3</th>
        <th>header4</th>
        <th>header5</th>
        <th>header6</th>
        <th>header7</th>
        <th>header8</th>
        <th>header9</th>
        <th>header10</th>
      </tr>
    </thead>
  </table>
</div>
<div id="div2">
  <table id="tablebody" class="table table-condensed table-striped small">
    <tbody>
      <tr>
        <td>data1</td>
        <td>data2</td>
        <td>data3</td>
        <td>data4</td>
        <td>data5</td>
        <td>data6</td>
        <td>data7</td>
        <td>data8</td>
        <td>data9</td>
        <td>data10</td>
      </tr>
    </tbody>
  </table>
</div>

在底部div 上滚动将滚动顶部div。将 jQuery 添加到 jsFiddle。

小提琴:https://jsfiddle.net/2xt1p8t7/

我在尝试回答自己的问题时遇到了这个问题。

略微扩展的解决方案。我试图以类似的方式链接两个div,以便一个与另一个一起滚动。这是初始代码(两个div 都有一个名为 joinscroll 的类)。

$('.joinscroll').on('scroll touchmove mousemove', function(e){
  if ($(this).attr("id") == "topdiv")  { $('#bottomdiv').scrollLeft($(this).scrollLeft()); }
  if ($(this).attr("id") == "bottomdiv")  { $('#topdiv').scrollLeft($(this).scrollLeft()); }
})

我遇到的问题是,在滚动过程中,浏览器检测到函数滚动的div 上的滚动,这导致为该div 的滚动执行函数。这导致了非常生涩的滚动,主要是因为有一个反馈循环。

我尝试了 preventDefault 和 stopPropagation 的技巧,但它们不起作用。

最后,最简单的解决方案是检测鼠标悬停在哪个div 上,并使用它来抑制其他函数:

$('.joinscroll').on('scroll touchmove mousemove', function(e){
if ($(this).is(':hover')) {
  if ($(this).attr("id") == "topdiv")  { $('#bottomdiv').scrollLeft($(this).scrollLeft()); }
  if ($(this).attr("id") == "bottomdiv")  { $('#topdiv').scrollLeft($(this).scrollLeft()); }
}  
})

希望这对某人有所帮助。

最新更新