I tested this, and the only way I could achieve a onscroll
event on a element is with an active scroll bar, which "floats". Explaining floats : it's position is at 100px from top, when it's scrolled we check if it went up or down, and then we bring it back to it's original position. So remember, you need an active scroll bar (the element's inner content should be higher than itself: you can use multiple <br>
etc...);
Javascript code:
<script type="text/javascript">
var defaultScrollTop = 100; /* testing value */
window.onload = function() {
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div1.scrollTop = defaultScrollTop; // Initialize to float
div1.onscroll = function(event) {
if (defaultScrollTop < div1.scrollTop) {
div2.scrollTop += 100; //scrollDown
} else if (defaultScrollTop > div1.scrollTop) {
div2.scrollTop -= 100; //scrollUp
}
div1.scrollTop = defaultScrollTop;
}
}
</script>
The CSS:
<style>
#div1, #div2 {
max-height: 100px; /* For testing */
overflow-y: scroll;
}
</style>
So, when you scroll inside div1
, it checks if it was scrolled up/down, scrolls div2
up/down and sets div1 to it's default scroll value. Hope this trick helps.
PS: You can make the scrollbars invisible if you make some research (overflow:hidden
is not working because there won't be active scrollbars)