I have a 3-column web page. The left and right columns have fixed width, and the central column uses the remaining width available. I’ve included a simplified version of it in the snippet below.
All columns are set to overflow: auto, to allow the user to scroll vertically.
I would like to make it so that when the user’s mouse is inside the right column, once they scroll to the bottom of that column, the central column will start to scroll.
The reason for this is that the right column will often not have enough content to need to scroll vertically. When this is the case, the web page will appear "broken" if the user happens to have their mouse over the right column, and tries to scroll down with their mouse wheel.
Is there a way to achieve this, and does it require javascript?
Note: the web version of X (Twitter) is exactly how I’d like it to work, but I’m not quite sure how that site is achieving it.
function addText(id) {
let elem = document.getElementById(id);
for(let i=0; i<50; i++){
let child = document.createElement('div');
child.innerText = `${i%10}`.repeat(200);
elem.appendChild(child);
}
}
['a','b','c'].forEach(addText);
.container {
display: flex;
position: fixed;
inset: 0;
background-color: green;
width: 100%;
}
.col {
position: relative;
height: 100%;
overflow: auto;
}
#a {
width: 200px;
background-color: blue;
}
#b {
width: 100%;
background-color: lightgrey;
}
#c {
width: 200px;
background-color: red;
}
<div class="container">
<div class="col" id="a"></div>
<div class="col" id="b"></div>
<div class="col" id="c"></div>
</div>
2
Answers
To achieve the desired scrolling effect, you should listen for the wheel event and use WheelEvent.deltaY. If the other columns have been scrolled to their bottom, add this value to the scrollTop property of the center column. It’s also important to check if WheelEvent.deltaY is positive or negative. This way, you can prevent the center column from scrolling when transitioning from bottom to top, and vice versa.
Please note: I’m not sure if this solution is compatible with mobile devices.
JS (+JQuery [optional])
HTML
You can do this without js if the sidebars have a fixed position. Somehow:
P.S. You should also note that the
mousewheel
event will not work on touch devices and mobile devices