I have a huge table where I want to introduce scrolling for the data. I don't want to separate into two tables, but scroll the data simultaneously. Here is a simplified code in JSFiddle and below:
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>
The CSS is as follows:
#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;
}
And the jQuery is:
$(function(){
$('.linked').scroll(function(){
$('.linked').scrollTop($(this).scrollTop());
})
})
Is it possible to make the two scrolling sections scroll simultaneously. As mentioned above, I don't want to have to touch the table structure, since it would be a nightmare.
You need to use scrollLeft() instead of scrollTop().
scrollLeft() is for horizontal scroll while scrollTop() is for vertical scroll.
$(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>
$('.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>
You can use jQuery's scroll functions.
Explanation:
| 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 |
When the top one is scrolled, this happens: