In the following snippet I have to div
s. I want to sync their position when I try to scroll any of them. When the second div is scrolled, I'm changing transform
of the first one and it works. But the first div is never scrolled. I've tried to pass mousewheel
event to the second one, but it didn't work.
And I can't make section
scrollable - it's just a simplified example.
// This code works
// `.labels` moves together with scroll of `.scrollable`
document.querySelector(".scrollable").addEventListener('scroll', function (e) {
var dy = this.scrollTop;
document.querySelector(".labels").style.transform = "translateY(" + -dy + "px)";
});
// This code does NOT work
// I want `.scrollable` to be scrolled when I try to scroll `.labels`
document.querySelector(".labels").addEventListener('wheel', function (e) {
console.log(Date.now());
document.querySelector(".scrollable").dispatchEvent(new MouseEvent(e.type, e));
});
section {
border: 1px solid red;
line-height: 2em;
height: 8em;
display: inline-block;
overflow: hidden;
white-space: nowrap;
}
div {
display: inline-block;
vertical-align: top;
padding: 0 .25em;
}
.scrollable {
max-height: 100%;
overflow: auto;
}
.labels {
background: silver;
}
.as-console-wrapper.as-console-wrapper {
max-height: 70px;
}
<section>
<div class="labels">
Label 1<br>
Label 2<br>
Label 3<br>
Label 4<br>
Label 5<br>
Label 6<br>
Label 7<br>
</div><div class="scrollable">
Line 1<br>
Line 2<br>
Line 3<br>
Line 4<br>
Line 5<br>
Line 6<br>
Line 7<br>
</div>
</section>