I need to scroll one specific element on the page even when the mouse isn’t hovering over that element…
I would normally put the div that needs scrolling on the top layer with a transparent background but I have a navigation menu that needs to be seen/interacted with on the same page.
In my experience CSS is extremely finicky; one change that is supposed to be aesthetic only ends up altering how the elements interact with each other and it is frustrating. I am hours into this project and I really don’t feel like starting from scratch again just for one bug like this… and having to test if the website works after every CSS addition.
I’ll put a jsfiddle of what my website is pretty much structured like.
html {
overflow: hidden;
}
.fixed {
position: fixed;
background-color: blue;
width: 50%;
}
.scrollable {
background-color: red;
height: 100vh;
padding-left: 50%;
overflow-y: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="fixed">
<h1>
Lorem ipsum dolor sit amet
</h1>
</div>
<div class="scrollable">
<ul>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
<li>_________</li>
</ul>
</div>
</body>
</html>
I can scroll anywhere on the red div and it works, but if I scroll on the blue div the red div won’t scroll. Changing the blue div to pointer-events: none;
makes it all work (this was the only answer I could find online) except for the fact that I have a navigation bar with pointer events inside the blue div. You would think after being around for centuries HTML would find a simple way to let a fixed element have pointer events AND scroll events!
TL;DR Is there a way to make a scroll anywhere on the screen scroll one specific div? Thanks in advance.
2
Answers
Building off of 0stone0's answer (with some added stuff to make the scroll more smooth and testing all the pointer events).
You could use the
wheel
event to capture ‘scroll’s on nonscrollable elements, and then translate that to the fixedscrollTop