【问题标题】:How to scroll items simultaneously when they are within a table?当它们在表格中时如何同时滚动项目?
【发布时间】:2021-11-24 01:07:14
【问题描述】:

我有一个巨大的表格,我想在其中引入数据滚动。我不想分成两个表,而是同时滚动数据。这是 JSFiddle 及以下的简化代码:

https://jsfiddle.net/oy4pdz8t/7/

<table>
<tr>
<td>fixed</td>
  <td>
    <div id="scrolling1" class=" linked">
      <div class="data">1</div>
      <div class="data">2</div>
      <div class="data">3</div>
      <div class="data">4</div>
      <div class="data">5</div>
      
    </div>
  </td>
</tr>
<tr>
<td>fixed</td>
  <td>
    <div  id="scrolling2" class=" linked">
      <div class="data">1</div>
      <div class="data">2</div>
      <div class="data">3</div>
      <div class="data">4</div>
      <div class="data">5</div>
      
    </div>
  </td>
</tr>
</table>

CSS如下:

#scrolling1{
  display: flex;
  overflow: auto;
  width: 100px;
  background: yellow;
  height: 50px;
  align-items: center;
}

#scrolling2{
  display: flex;
  overflow: auto;
  width: 100px;
  background: green;
  height: 50px;
  align-items: center;
}

.data{
  display: flex;
  flex: 0 0 40px;
  background: red;
  
}

jQuery 是:

$(function(){

    $('.linked').scroll(function(){
        $('.linked').scrollTop($(this).scrollTop());    
    })

})

是否可以让两个滚动部分同时滚动。如上所述,我不想碰表结构,因为那将是一场噩梦。

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    您需要使用scrollLeft() 而不是scrollTop()scrollLeft() 用于水平滚动,scrollTop() 用于垂直滚动。

    $(function() {
    
      $('.linked').scroll(function() {
        $('.linked').scrollLeft($(this).scrollLeft());
      })
    
    })
    #scrolling1 {
      display: flex;
      overflow: auto;
      width: 100px;
      background: yellow;
      height: 50px;
      align-items: center;
    }
    
    #scrolling2 {
      display: flex;
      overflow: auto;
      width: 100px;
      background: green;
      height: 50px;
      align-items: center;
    }
    
    .item {
      display: flex;
      flex: 0 0 40px;
      background: red;
    }
    <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
    <table>
      <tr>
        <td>fixed</td>
        <td>
          <div id="scrolling1" class=" linked">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
    
          </div>
        </td>
      </tr>
      <tr>
        <td>fixed</td>
        <td>
          <div id="scrolling2" class=" linked">
            <div class="item">1</div>
            <div class="item">2</div>
            <div class="item">3</div>
            <div class="item">4</div>
            <div class="item">5</div>
    
          </div>
        </td>
      </tr>
    </table>

    【讨论】:

    • 哦!使用的变量比我少一个。你赢了!
    • 两个答案都对我有同样的帮助,必须选择一个。在一个额外的步骤中显示变量实际上向我解释了链接。谢谢先生们!!
    【解决方案2】:

    $('.linked').on('scroll', function() {
      var y = $(this).scrollLeft();
      $('.linked').scrollLeft(y);
    });
    #scrolling1{
      display: flex;
      overflow: auto;
      width: 100px;
      background: yellow;
      height: 50px;
      align-items: center;
    }
    
    #scrolling2{
      display: flex;
      overflow: auto;
      width: 100px;
      background: green;
      height: 50px;
      align-items: center;
    }
    
    .data{
      display: flex;
      flex: 0 0 40px;
      background: red;
      
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
    <tr>
    <td>fixed</td>
      <td>
        <div id="scrolling1" class="linked">
          <div class="data">1</div>
          <div class="data">2</div>
          <div class="data">3</div>
          <div class="data">4</div>
          <div class="data">5</div>
          
        </div>
      </td>
    </tr>
    <tr>
    <td>fixed</td>
      <td>
        <div  id="scrolling2" class="linked">
          <div class="data">1</div>
          <div class="data">2</div>
          <div class="data">3</div>
          <div class="data">4</div>
          <div class="data">5</div>
          
        </div>
      </td>
    </tr>
    </table>

    你可以使用jQuery的滚动功能。

    解释:

    Line Explanation
    $('.linked').on('scroll', function() { Activate on scroll of either of the .linkeds
    var y = $(this).scrollLeft(); Save the scroll position
    $('.linked').scrollLeft(y); Scroll both of them to that position
    }); Close the block

    当滚动顶部时,会发生这种情况:

    【讨论】:

      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 2011-06-25
      相关资源
      最近更新 更多