Modern CSS allows us to detect scroll behavior and apply styles without using JavaScript. In this example, we check whether the .container element is scrollable. By using @keyframes and animation-timeline, we track the scroll state.
If the element is scrollable, the background turns green #008000; otherwise, it turns red #FF0000.
@keyframes detectScroll {
from,
to {
--scroll-status:
}
}
.container {
animation: detectScroll linear;
animation-timeline: scroll(self);
--bg-scrollable: var(--scroll-status) #008000;
--bg-scrollable-hide: #FF0000;
background-color: var(--bg-scrollable, var(--bg-scrollable-hide));
}
This method provides a simple and efficient way to give users visual feedback. CSS is now capable of handling interactions like scrolling!